I am having an issue getting my data to return. I have two times without the dates as in 00:00:00 and I am trying to compare them. The current on time is 09:00:00 and the off time is currently 02:00:00. The time being compared against, as in my SQL is 18:21:00 which is between the on and off times but I can’t get the results.
Here is my SQL:
SELECT zdate
FROM zones
WHERE '18:21:00' BETWEEN time_on AND time_off
Note that ‘
x BETWEEN y AND z‘ assumesyis less thanz; it never returns anything otherwise. It is equivalent to ‘x >= y AND x <= z‘.Given that the off time is after midnight in this case, then you need to do a much more complex condition:
The first condition is the normal one for time on is on the same day as time off. The second alternative checks that the target time is after the time on (and implicitly before midnight) or that it is before the time off (and hence between midnight and the time off).
For greater symmetry:
Example output using IBM Informix Dynamic Server (IDS) 11.50.FC4W1 on MacOS X 10.6.2. IDS uses ‘DATETIME HOUR TO SECOND’ as the equivalent of the TIME type in standard SQL.
Output data:
I think that looks correct.