Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:50:31+00:00 2026-05-13T09:50:31+00:00

I am having an issue getting my data to return. I have two times

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:50:31+00:00Added an answer on May 13, 2026 at 9:50 am

    Note that ‘x BETWEEN y AND z‘ assumes y is less than z; 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:

    SELECT zdate
      FROM zones
     WHERE (time_on < time_off AND '18:21:00' BETWEEN time_on AND time_off)
        OR (time_on > time_off AND ('18:21:00' > time_on OR '18:21:00' < time_off))
    

    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:

    SELECT zdate
      FROM zones
     WHERE (time_on < time_off AND ('18:21:00' > time_on AND '18:21:00' < time_off))
        OR (time_on > time_off AND ('18:21:00' > time_on OR  '18:21:00' < time_off))
    

    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.

    CREATE TEMP TABLE zones
    (
        time_on  DATETIME HOUR TO SECOND NOT NULL,
        time_off DATETIME HOUR TO SECOND NOT NULL
    );
    INSERT INTO zones VALUES ('09:00:00', '02:00:00');
    INSERT INTO zones VALUES ('07:00:00', '19:00:00');
    INSERT INTO zones VALUES ('20:00:00', '22:00:00');
    INSERT INTO zones VALUES ('10:00:00', '15:15:00');
    INSERT INTO zones VALUES ('21:00:00', '04:00:00');
    
    CREATE TEMP TABLE times
    (
        zdate DATETIME HOUR TO SECOND NOT NULL
    );
    INSERT INTO times VALUES ('18:21:00');
    INSERT INTO times VALUES ('08:30:00');
    INSERT INTO times VALUES ('20:30:00');
    INSERT INTO times VALUES ('12:30:00');
    
    SELECT zdate, time_on, time_off
      FROM zones CROSS JOIN times
     WHERE (time_on < time_off AND (zdate > time_on AND zdate < time_off))
        OR (time_on > time_off AND (zdate > time_on OR  zdate < time_off))
     ORDER BY zdate, time_on, time_off
    

    Output data:

    08:30:00   07:00:00   19:00:00
    12:30:00   07:00:00   19:00:00
    12:30:00   09:00:00   02:00:00
    12:30:00   10:00:00   15:15:00
    18:21:00   07:00:00   19:00:00
    18:21:00   09:00:00   02:00:00
    20:30:00   09:00:00   02:00:00
    20:30:00   20:00:00   22:00:00
    

    I think that looks correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.