Solved

SQL:   Data Sets - Reserved vs Available Dates

Posted on 2007-11-29
4
189 Views
Last Modified: 2010-04-21
I have a table with reservation dates.  I would like to create a query that returns the dates not in the reservation table...for a given date range.  Is there a simple way to do this without.

I basicly need to return a set of 'interesting' dates....and the interest is availability.
0
Comment
Question by:Paulconsulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 20376880
SELECT dt
FROM Calendar
WHERE dt >= '20070101'
 AND dt < '20080101'
EXCEPT
SELECT dt
FROM Reservations;
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 20377268
SELECT c.date
FROM calendar c
LEFT OUTER JOIN reservations r ON c.date = r.date
WHERE r.date IS NULL
AND c.date BETWEEN 'startDate' AND 'endDate'
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 125 total points
ID: 20377363
The simplest way is to use a calendar table or, more versatilely, a numbers table with DATEDIFF.  This example uses a numbers table with numbers from 0 to 8192 (which should be a long enough range).

CREATE TABLE Numbers (value int primary key)

INSERT INTO Numbers Values (0)
Insert Into Numbers Values (1)

INSERT INTO Numbers SELECT Value + 2 FROM Numbers
INSERT INTO Numbers SELECT Value + 4 FROM Numbers
INSERT INTO Numbers SELECT Value + 8 FROM Numbers
INSERT INTO Numbers SELECT Value + 16 FROM Numbers
INSERT INTO Numbers SELECT Value + 32 FROM Numbers
INSERT INTO Numbers SELECT Value + 64 FROM Numbers
INSERT INTO Numbers SELECT Value + 128 FROM Numbers
INSERT INTO Numbers SELECT Value + 256 FROM Numbers
INSERT INTO Numbers SELECT Value + 512 FROM Numbers
INSERT INTO Numbers SELECT Value + 1024 FROM Numbers
INSERT INTO Numbers SELECT Value + 2048 FROM Numbers
INSERT INTO Numbers SELECT Value + 4096 FROM Numbers


Now that we have our table, let's see what we can do with it:

Assume that our interested date range is from Jan 15, 2007 to June 15, 2007

SELECT Dateadd(Day, N.Value, '20070115')
FROM Numbers N
LEFT JOIN ReservationDates rd
    ON Dateadd(Day, N.Value, '20070115') = rd.ReservedDate
WHERE N <= Datediff(Day, '20070115', '20070615')
    AND rd.ReservedDate is null

Clearly, a table with all dates would also work.  But the numbers table allows for more versatility with a smaller table (need to query a range back in 1980?  No need to have all of the dates in the table).
0
 

Author Closing Comment

by:Paulconsulting
ID: 31411776
Works great!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question