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

  • SEARCH
  • Home
  • 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 6825791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:04:24+00:00 2026-05-26T22:04:24+00:00

Assuming I have table called events with the columns INT id DATETIME start_time DATETIME

  • 0

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.

  • 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-26T22:04:24+00:00Added an answer on May 26, 2026 at 10:04 pm

    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:

    public void checkEvents (int[][] events, int startHour, int endHour)    
    
      final int START         = 0;
      final int END           = 1;
      final int HOURS_PER_DAY = 24;
    
      for (int[] event : events) {
    
        int start = startHour;
        int end = (endHour < startHour) ? endHour + HOURS_PER_DAY : endHour;
    
        int eventStart = event[START];
        int eventEnd = (event[END] < event[START]) ? event[END] + HOURS_PER_DAY : event[END];
    
        if (checkOverlap(result, event, start, end, eventStart, eventEnd))
        || (checkOverlap(result, event, start, end, eventStart + HOURS_PER_DAY, eventEnd + HOURS_PER_DAY))
          || (checkOverlap(result, event, start + HOURS_PER_DAY, end + HOURS_PER_DAY, eventStart, eventEnd))
           || (!checkOverlap(result, event, start + HOURS_PER_DAY, end + HOURS_PER_DAY, eventStart + HOURS_PER_DAY, eventEnd + HOURS_PER_DAY);
              System.out.println("overlapped!");
      }
    }
    
    private boolean checkOverlap(ArrayList<int[]> result, int[] event, int start, int end, int eventStart, int eventEnd) {
    
      if ((eventStart >= start && eventStart < end) || (eventStart < start && eventEnd > start)) {
        return true;
      }
    
      return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have an employee table with 2 columns only: employee_id manager_id All employees
Assuming you have the following database table: create table Names ( Id INT IDENTITY
Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT
Assuming I have a table Entities with the columns Value and Type as varchar(strings)
Assuming I have a table with one field called ID that stores 100 different
I have a table called ORDEREXECUTIONS that stores all orders that have been executed.
Assuming I have a class A as follows: class A{ int id; int getId(){};
Assuming I have a column called A and I want to check if A
I have a table of events (in a sqlite3 database for what it's worth)
I have a mysql table called pollOfTheWeek. It has a column pollDate of type

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.