我有datetime
这样的:01-Jan-2014 16:30
如何在任何年,月或日中搜索以下between
次的记录:
SELECT *
FROM TABLE
WHERE ORDERDATE BETWEEN '01-Jan-2014 16:30' AND '01-Jan-2014 17:00'
答案 0 :(得分:2)
DECLARE @F time='16:30'
DECLARE @L time='17:00'
SELECT *
FROM TABLE
WHERE cast(ORDERDATE as time) BETWEEN @F AND @L
答案 1 :(得分:1)
我认为最简单的方法可能是:
where extract(hour from orderdate) * 100 + extract(minute from orderdate) between 1630 and 1700
这会将时间转换为HHMM形式的数字,可以与between
进行比较。
您也可以将其写为:
where hour(orderdate) * 100 + minute(orderdate) between 1630 and 1700
答案 2 :(得分:1)
使用cast
从日期时间字段中提取时间的查询
where cast(ORDERDATE as time) between '16:30' and '17:00'