2012-04-24 43 views
1

我有這樣T-SQL時間邏輯

storeID starttime endtime 
    A  1:00AM 4:00PM 
    B  7:30PM 6:00AM 
    C  2:00AM 4:00AM 

3個數據我要檢查的給定的時間落在兩個時間間隔之間。

你能爲該邏輯推薦一個T-SQL語句嗎?

如果假設我想獲得在凌晨1點營業的商店,我必須得到A, B

回答

1
declare @testTime time 
select @testTime = '01:00' 
select storeId from openinghours 
    where (startTime<=endTime and @testTime >=startTime and @testTime <=endTime) 
     OR (startTime>endTime and (@testTime <startTime OR @testTime > endTime)) 
0
declare @testTime time 
select @testTime = '01:00' 
select storeId from openinghours where @testime between starttime and endtime