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 4606320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:32:58+00:00 2026-05-22T00:32:58+00:00

I need to find whether a new schedule overlaps any existing schedules. This is

  • 0

I need to find whether a new schedule overlaps any existing schedules.

This is the “intervals” table:

     id  first              last    
     1   1900-01-01 09:00   1900-01-01 10:00
     2   1900-01-01 15:00   1900-01-01 18:00
     3   1900-01-01 18:01   1900-01-01 08:00

I am using a scalar function dbo.TimeOnly for extracting time part from the datetime fields.

My selection criteria as follows

First case

     declare @start datetime
     declare @end datetime

     set @start = dbo.TimeOnly('2011-may-11 08:01:00');
     set @end = dbo.TimeOnly('2011-may-11 15:30:00');

     select  * from intervals where 
     ( NOT ( dbo.TimeOnly(last) < @start OR  @end < dbo.TimeOnly(first) ) )

This will return 1st and 2nd records. I got this logic from Check whether the schedule overlaps each other?

Second case

    set @start = dbo.TimeOnly('2011-may-11 07:01:00');
    set @end = dbo.TimeOnly('2011-may-11 08:30:00');

How do I write a query that will return only the 3rd record for the criteria in the second case?

UPDATE

I will give more details for my problem

Different people are managing a particular event for a certain time duration in a day.

For Monday, the schedule format is like this

    Id  Start       End          User   Days    
     1  00:01 AM    08:00 AM     'A'    1
     2  08:01 AM    04:00 PM     'B'    1
     3  04:00 PM    00:00 AM     'C'    1

For Tuesday’s

     4  08:01 AM    04:00 PM     'B'    2
     5  07:00 PM    07:00 AM     'C'    2

For Wednesday’s

     6  08:01 AM    04:00 PM     'A'    4
     7  10:00 PM    08:00 AM     'B'    4

Here days are stored in the bit value format ie

Monday=1,Tuesday=2,Wednesday=4,Thursday=8,Friday=16,Saturday=32 and Sunday=64

When we creating a schedule for a particular day, it should not overlap between times.
I would like to get a SQL query for checking any schedules exists while creating a new schedule for a particular day.

For a particular event time (Say An even occured at 04:00 AM on Tuesday) I would like to find the correct schedule (Will be “5”) that falls between the Start and End time.

  • 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-22T00:32:59+00:00Added an answer on May 22, 2026 at 12:32 am

    Change your SELECT to this:

    select  * from intervals where 
    (
    ( dbo.TimeOnly(last) > dbo.TimeOnly(first)
    AND
    NOT (dbo.TimeOnly(last) < @start OR  @end < dbo.TimeOnly(first)) )
    
    OR
    
    ( dbo.TimeOnly(last) < dbo.TimeOnly(first)
    AND
    ( @start >= dbo.TimeOnly(first) OR @end <= dbo.TimeOnly(last) OR (@start < dbo.TimeOnly(first) AND @end > dbo.TimeOnly(last)) ) )
    )
    

    I might’ve missed a parenthesis somewhere, but I hope not.

    The concept here is a query with 2 main groupings combined with an OR. The first clause checks intervals where last > first and is mainly a copy of your existing query with the addition of the last > first condition, while the 2nd clause checks intervals where last < first.

    In the case where last < first, there are 3 ways that an interval can overlap:

    • start is after the interval’s first
    • end is before the interval’s last
    • start and end completely engulf the interval, i.e., start is before first and end is after last

    Any one of these 3 conditions would mean the schedule to check is within an existing interval, so the 3 conditions are combined with ORs.

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

Sidebar

Related Questions

I need to find whether a new event created over laps any of the
I need to find out whether my apps are being flagged as viruses by
I have a string and I need to find out whether it is a
I need to find out, in code (C++ on Windows 7), whether my video
Need to find the timestamp for the first minute of the first day of
I need to find whether a word is verb or noun or it is
I'm not sure whether this is new in WinRT or existed before; in IDL,
During the new user registration process, I'm trying to find whether a user name
I need to find whether a computer with a given Guid exists inside a
I need to create a stored procedure that upon exceution checks if any new

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.