fault_final
fault_final

Reputation: 23

Filtering in SQL between evening previous day and morning current day

I am trying to filter a table between 4 pm of previous day and 4am of current day but am at loss of how to query that.

Something like:

WHERE
dateColumn >= DATEADD(DAY, -1, GETDATE()) AND dateColumn <= GETDATE()
AND DATEPART(hh, dateColumn) >= 16 AND DATEPART(hh, dateColum) <= 4

I realize the second line in WHERE statement is obviously incorrect and will not return any results but that is to give an idea of what I am trying to do. All help is appreciated!

Upvotes: 0

Views: 664

Answers (1)

norie
norie

Reputation: 9867

There are various ways you could do this, this gets the datetime at 1600 yesterday and 1600 today.

WHERE dataColumn >= dateadd(hour, -8, convert(datetime, convert(date, getdate()))) 
AND   dateColumn <= dateadd(hour, 16, convert(datetime, convert(date, getdate())));

Upvotes: 1

Related Questions