I have a table with two fields that describe opening hours of each store – “from” and “to” in 24h format integers. I created a search page that allows user to find stores that are open at given hour.
I don’t really know how to create the SQL query, the main problem is that there are two cases.
One case is that from value is bigger than to value, for example from 23 to 5 and it gives two intervals 23 to 24 and 1 to 5.
Second case is when to value is bigger than from value (say from 8 to 22), where I get one interval.
Any idea how to create the query, that would work in those two cases?
Let’s supose this table schema:
and a parameter @T (the hour you are looking for)
Then select is:
Explaining
Select all fields from stores that: