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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:26+00:00 2026-05-25T21:03:26+00:00

Table 1 code StartDate EndDate A 01/01/2011 06/15/2011 A 06/25/2011 06/30/2011 B 01/12/2011 07/31/2011

  • 0

Table 1

code        StartDate        EndDate
A           01/01/2011        06/15/2011
A           06/25/2011        06/30/2011
B           01/12/2011        07/31/2011
B           08/3/2011         12/31/2011

Table 2

code        StartDate        EndDate
A           01/01/2011       06/30/2011
B           01/12/2011        07/31/2011
B           08/3/2011         12/25/2011

I need find what is in Table1 and not in Table2 which is

code        StartDate        EndDate
B           12/26/2011        12/31/2011

And the Converse of what is in Table2 and not in Table1

code        StartDate        EndDate
A           06/16/2011       06/29/2011

There is no time component in the date field and T-SQL (SQL Server 2000 ) is preferred.

  • 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-25T21:03:27+00:00Added an answer on May 25, 2026 at 9:03 pm

    This is a bit tedious in SQL Server 2000. It uses a numbers table to expand out the ranges of dates into individual rows. Then NOT EXISTS for the anti semi join then uses an “islands” approach I stole from an MSDN article to collapse the result back into ranges again.

    SET DATEFORMAT MDY
    
    DECLARE @Numbers TABLE (N INT PRIMARY KEY)
    
    INSERT INTO @Numbers
    SELECT number
    FROM master..spt_values
    WHERE type='P' AND number >= 0
    
    
    DECLARE @Table1 TABLE
    (
    code CHAR(1),
    StartDate DATETIME,
    EndDate DATETIME
    )
    DECLARE @Table2 TABLE
    (
    code CHAR(1),
    StartDate DATETIME,
    EndDate DATETIME
    )
    INSERT INTO @Table1
    SELECT 'A','01/01/2011','06/15/2011' UNION ALL
    SELECT 'A','06/25/2011','06/30/2011' UNION ALL
    SELECT 'B','01/12/2011','07/31/2011' UNION ALL
    SELECT 'B','08/3/2011',' 12/31/2011'
    
    INSERT INTO @Table2
    SELECT 'A','01/01/2011','06/30/2011' UNION ALL
    SELECT 'B','01/12/2011','07/31/2011' UNION ALL
    SELECT 'B','08/3/2011',' 12/25/2011'
    
    DECLARE @Results TABLE
    (
    code CHAR(1),
    StartDate DATETIME
    )
    
    INSERT INTO @Results
    SELECT T1.code,
           DATEADD(DAY, N, StartDate)
    FROM   @Table1 T1
           INNER JOIN @Numbers N1
             ON N <= DATEDIFF(DAY, StartDate, EndDate)
    WHERE  NOT EXISTS (SELECT *
                       FROM   @Table2 T2
                              INNER JOIN @Numbers N2
                                ON N2.N <= DATEDIFF(DAY, T2.StartDate, T2.EndDate)
                       WHERE  DATEADD(DAY, N1.N, T1.StartDate) =
                              DATEADD(DAY, N2.N, T2.StartDate)
                              AND T1.code = T2.code)  
    
    /*SQL Server 2000 gaps and islands approach from here 
    http://msdn.microsoft.com/en-us/library/aa175780%28v=sql.80%29.aspx*/
    SELECT t1.code,
           t1.StartDate,
           MIN(t2.StartDate) AS EndDate
    FROM   (SELECT StartDate,
                   code
            FROM   @Results tbl1
            WHERE  NOT EXISTS(SELECT *
                              FROM   @Results tbl2
                              WHERE  tbl1.StartDate = tbl2.StartDate + 1
                                     AND tbl1.code = tbl2.code)) t1
           INNER JOIN (SELECT StartDate,
                              code
                       FROM   @Results tbl1
                       WHERE  NOT EXISTS(SELECT *
                                         FROM   @Results tbl2
                                         WHERE  tbl2.StartDate = tbl1.StartDate + 1
                                                AND tbl1.code = tbl2.code)) t2
             ON t1.StartDate <= t2.StartDate
             AND t1.code = t2.code
    GROUP  BY t1.code,
              t1.StartDate  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code: CREATE TABLE IF NOT EXISTS Person ( name varchar(24) ...
Should I still be using tables anyway? The table code I'd be replacing is:
i have a table with code like this: <tr> <th scope=row>5-17</th> <td>23.6 (22.0-24.0)</td> <td>18.0
I'm using a hash table (source code by Google Inc) to store some method
I am trying to access an HTML table from code behind, and set its
I have a table defined (see code snippet below). How can I add a
How to display a table order by code (like 01, 02… then null columns)?
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have a table which holds the guid for an user and their actual

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.