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.
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 EXISTSfor the anti semi join then uses an “islands” approach I stole from an MSDN article to collapse the result back into ranges again.