I am looking for an elegant way to select records that fall between a start and end time.
I have a datetime
field that hold the date and time of the record. I can get the time by CAST(fieldname as time)
.
A BETWEEN
does not work if you are looking for records that occurred between 22:00 and 03:00
Ideally I would like to use a CASE
in the WHERE
clause
CASE
WHEN @Start < @End THEN CAST(fieldname as time) BETWEEN @Start AND @END
ELSE (CAST(fieldname as time) > @Start OR CAST(fieldname as time) < @End)
END
Any sugestions how I could do this in a single elegant way.
Thanks