Вот базовое решение, которое, я думаю, вполне может быть использовано в качестве отправной точки:
DECLARE @Today datetime = CAST(GETDATE() AS date);
WITH datasource (TimeRange) AS (
SELECT '10 a.m. - 1 p.m.' UNION ALL
SELECT '8 am - 12 noon' UNION ALL
SELECT '12 noon - 4 p.m.' UNION ALL
SELECT '9 - 10 am' UNION ALL
SELECT '10 am 1 pm'
),
HyphensIncluded AS (
SELECT *, HyphenPos = CHARINDEX('-', TimeRange)
FROM datasource
WHERE CHARINDEX('-', TimeRange) > 0
),
Split AS (
SELECT
*,
StartTimeStr = REPLACE(
REPLACE(
LTRIM(RTRIM(SUBSTRING(TimeRange, 0, HyphenPos))),
'.',
''
),
'noon',
'pm'
),
EndTimeStr = REPLACE(
REPLACE(
LTRIM(SUBSTRING(TimeRange, HyphenPos + 1, LEN(TimeRange) - HyphenPos)),
'.',
''
),
'noon',
'pm'
)
FROM HyphensIncluded
),
Validated AS (
SELECT *
FROM Split
WHERE ISDATE(StartTimeStr) = 1
AND ISDATE(EndTimeStr) = 1
)
SELECT
StartTimestamp = @Today + StartTimeStr,
EndTimestamp = @Today + EndTimeStr
FROM Validated
Вывод:
StartTimestamp EndTimestamp
----------------------- -----------------------
2011-05-23 10:00:00.000 2011-05-23 13:00:00.000
2011-05-23 08:00:00.000 2011-05-23 12:00:00.000
2011-05-23 12:00:00.000 2011-05-23 16:00:00.000