从SQL Server中的getdate()中选择特定的小时和分钟

时间:2019-05-22 11:02:11

标签: sql-server hour getdate minute

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

上面是用于从9AM到03:30 PM到1PM除外之间获取数据的查询。

但是我需要修改此查询以获取介于09:30 AM到03:30 PM以及1PM以外的数据。

有人可以帮我吗?

5 个答案:

答案 0 :(得分: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'))

答案 1 :(得分:0)

参考您的查询。

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

答案 2 :(得分:0)

类似于您的实现。

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

  • 第二个条件,时间为3.00 PM至3.30 PM

  • 9.30 AM至9.59 AM之间的第三种条件

示例:

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

注意:不包括例外情况。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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)