从特定时间选择数据

时间:2012-11-01 16:18:15

标签: sql-server datetime

我无法获取仅在昨天早上6点到今天早上6点之间显示结果的查询。使用sql server 2008.我有一个timestamp列,其类型为datetime

2 个答案:

答案 0 :(得分:5)

select * from your_table
where ts_col between 
             dateadd(hour, 6, DATEADD(day, DATEDIFF(day, 0, GETDATE()), -1))   
             and dateadd(hour, 6, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0));

SQLFiddle example

答案 1 :(得分:0)

where DATEDIFF(SECOND, CAST(GETDATE() as DATE), ts_column) BETWEEN -21600 AND 21600