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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:40:22+00:00 2026-05-15T01:40:22+00:00

I have a table with start and finish datetimes that I need to determine

  • 0

I have a table with start and finish datetimes that I need to determine if any overlap and not quite sure the best way to go.

Initially I was thinking of using a nested cursor as shown below which does work, however I’m checking the same records against each other twice and I’m sure it is not very efficient.

eg: this table would result in an overlap.

id  start                       end
-------------------------------------------------------
1   2009-10-22 10:19:00.000     2009-10-22 11:40:00.000
2   2009-10-22 10:31:00.000     2009-10-22 13:34:00.000
3   2009-10-22 16:31:00.000     2009-10-22 17:34:00.000

Declare @Start datetime, @End datetime, @OtherStart datetime, @OtherEnd datetime, @id int, @endCheck bit

Set @endCheck = 0

DECLARE Cur1 CURSOR FOR
        select id, start, end from table1 

OPEN Cur1
FETCH NEXT FROM Cur1 INTO @id, @Start, @End
WHILE @@FETCH_STATUS = 0 AND @endCheck = 0
BEGIN
    -- Get a cursor on all the other records
    DECLARE Cur2 CURSOR FOR
            select start, end from table1 
                and id != @id AND start < @end
    OPEN Cur2
    FETCH NEXT FROM Cur2 INTO @OtherStart, @OtherEnd
    WHILE @@FETCH_STATUS = 0 AND @endCheck = 0
    BEGIN

            if ( @Start > @OtherStart AND @Start < @OtherEnd    OR
                 @End > @OtherStart AND @End < @OtherEnd )
                or
               ( @OtherStart > @Start AND @OtherStart < @End    OR
                 @OtherEnd > @Start AND @OtherEnd < @End )

            BEGIN
                SET @endCheck = 1
            END

            FETCH NEXT FROM Cur2 INTO @OtherStart, @OtherEnd
    END
    CLOSE Cur2
    DEALLOCATE Cur2

    FETCH NEXT FROM Cur1 INTO @id, @Start, @End
END
CLOSE Cur1
DEALLOCATE Cur1
  • 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-15T01:40:22+00:00Added an answer on May 15, 2026 at 1:40 am
    • First, let’s exhaustively examine what an “overlap” means and optimize your boolean expression.

    An overlap is any of the following conditions:

                     Start1                     End1
    ---------------------------------------------------------------------------------
     Start2                       End2
     Start2                                                       End2
                                 Start2                           End2
    

    And is NOT any of the following conditions:

                     Start1                     End1
    ---------------------------------------------------------------------------------
     Start2  End2
                                                          Start2  End2
    

    If you look carefully, you’ll notice Start2 < End1 AND End2 > Start1 defines this relationship, so you can reduce your expression to this.


    • Second, you can put this in a WHERE condition for your cursor instead of looping through every row and checking.

    • Third, since you’re just checking whether something exists or not, you can use an IF EXISTS clause instead of looping through a cursor and checking.

    • Finally, you can condense this all down to a single query using an INNER JOIN on itself or WHERE EXISTS, depending on how you want your final output to look. To get all the overlaps in the table, you can try something like the following (the t2.id > t1.id is to only get each overlap once):

      SELECT t1.id, t2.id
      FROM MyTable t1
      INNER JOIN MyTable t2 ON t2.id > t1.id 
                            AND t2.start < t1.end AND t2.end > t1.start
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a Word document that starts with a Table of Contents. I need
I have decided in my table that I no longer want to record start
I have periods table that has start_at:datetime end_at:datetime . Because each period begins and
I have a table of events with a recorded start and end time as
I have two schemas S1 and S2 with identical table structure. I`ll start with
I'm seeing basically the same issue described here I have a table that starts
I have two tables, both with start time and end time fields. I need
I have a table that looks something like this: word big expensive smart fast
I have a table with 20,000 rows of data that I imported but I

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.