Select 来自 SQL 服务器中 getdate() 的特定小时和分钟

Select specific hour and minute from getdate() in SQL Server

((DATEPART(HOUR, getdate()) BETWEEN 9 AND 15) or (DATEPART(HOUR, getdate())= 15 and (DATEPART(MINUTE, getdate()) between 00 and 30))) 

以上是用于在上午 9 点到 03:30PM 和下午 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'))

参考您的查询。

((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)))

类似于您的实现。

  • 上午 10 点到下午 3 点之间的第一个条件

  • 下午 3 点到 3 点之间的第二个条件

  • 第三个条件是上午 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)))

注意:不包括例外情况。

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

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)