Assuming I have table called events with the columns
INT id
DATETIME start_time
DATETIME end_time
how would I find all rows where any part of the time range from start_time to end_time falls within a certain hour range?
For example, I may want to find rows where the events had some portion of them during the 8-10pm hour range.
Something like
select * from events where (hour(start_time) IN (20, 21, 22)) or (hour(end_time) IN (20, 21, 22))
would work, except for events that started before 8pm and ended after 11pm.
Lastly, it has to work across day boundaries, so a range like 23-2 (11pm – 2am) should also work.
Unfortunately I’m unable to modify the schema of the table.
I managed to make this work with the day boundaries requirement. It’s ugly, but it seems to cover all the cases.
While you could write a stored procedure to do it, it’s easier to express in Java, so: