Reputation: 23
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
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