silentbob
silentbob

Reputation: 59

Selecting data from specific times

I am having trouble getting a query that only displays results between 6am yesterday and 6am today. Using sql server 2008. I have a timestamp column with type datetime

Upvotes: 1

Views: 58

Answers (2)

IUnknown
IUnknown

Reputation: 22478

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

Upvotes: 0

juergen d
juergen d

Reputation: 204924

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

Upvotes: 5

Related Questions