考虑:
((DATEPART(HOUR, getdate()) BETWEEN 9 AND 15) or (DATEPART(HOUR, getdate())= 15 and (DATEPART(MINUTE, getdate()) between 00 and 30)))
以上是用于在上午 9 点到下午 03:30 以及除了下午 1 点之间获取数据的查询。
如何修改此查询以获取上午 09:30 到下午 03:30 之间(下午 1 点除外)之间的数据?
转换为
time
似乎是最简单的解决方案。
WHERE CONVERT(time,GETDATE()) >= '09:30:00' AND CONVERT(time,GETDATE())< '15:30:00'
AND NOT(CONVERT(time,GETDATE())>= '13:00:00' AND CONVERT(time,GETDATE())< '14:00:00')
或者你可以这样做:
WHERE ((CONVERT(time,GETDATE())>= '09:30:00' AND CONVERT(time,GETDATE())< '13:00:00')
OR (CONVERT(time,GETDATE())>= '14:00:00' AND CONVERT(time,GETDATE())< '15:30:00'))
与您的实现类似。
第一个条件是上午 10 点到下午 3 点
第二个条件为下午 3.00 至 3.30 之间
第三个条件为上午 9.30 至 9.59 之间
示例:
((DATEPART(HOUR, getdate()) BETWEEN 10 AND 15)
or (DATEPART(HOUR, getdate())= 15 and (DATEPART(MINUTE, getdate()) between 00 and 30))
or (DATEPART(HOUR, getdate())= 9 and (DATEPART(MINUTE, getdate()) between 30 and 59)))
注意:不包括例外情况。
select * from test12 where (DATEPART(HOUR,Trackdatetime) >= 09 and DATEPART(MINUTE,Trackdatetime) >=30)
and (DATEPART(HOUR,Trackdatetime) <= 15 and DATEPART(MINUTE,Trackdatetime) >=30)
except
select * from test12 where (DATEPART(HOUR,Trackdatetime) = 13 and DATEPART(MINUTE,Trackdatetime) = 00)
where cast(cast(getDate() as float) - floor(cast(getDate() as float)) as datetime)
between '09:30:00' and '15:30:00'
and datepart(hour, getDate())<>13
或
where convert(time, getDate())
between '09:30:00' and '15:30:00'
and datepart(hour, getDate())<>13
参考您的询问。
((DATEPART(HOUR, getdate()) BETWEEN 10 AND 14)
AND (DATEPART(HOUR, getdate()) <> 13)
or (DATEPART(HOUR, getdate())= 15 and (DATEPART(MINUTE, getdate()) between 00 and 30))
OR (DATEPART(HOUR, getdate())= 9 and (DATEPART(MINUTE, getdate()) between 30 and 59)))