我想编写SQL来检查表A中的给定时间(例如上午5:24)是否在表B中的以下不规则时间段之一内。
5:00 AM to 6:00 AM
7:30 AM to 9:15 AM
1:26 PM to 2:45 PM
4:00 PM to 4:59 PM
10:00 PM to 11:14 PM
我的计划是编写这个SQL语句:
CASE
WHEN [Given Time] BETWEEN [START TIME] and [END TIME] THEN 1
ELSE 0
END
但后来我意识到我需要写5个连接 - 还有另一种方法吗?
萨姆
答案 0 :(得分:0)
请试试这个:
CASE
WHEN EXISTS (SELECT 1 FROM [My Time Table]
WHERE [Given Time] BETWEEN [START TIME] and [END TIME])
THEN 1
ELSE 0
END
答案 1 :(得分:0)
我没有看到CASE
声明或JOIN
的单一原因。这应该足够了。
DECLARE @YourTime time = '05:42'
SELECT
*
FROM
YourTable
WHERE
@YourTime between [Start Time] and [End Time]
或者更明确地......
...
WHERE
[Start Time] <= @YourTime
AND [End Time] >= @YourTime