Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


SQL:   Data Sets - Reserved vs Available Dates

Posted on 2007-11-29
Medium Priority
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.
Question by:Paulconsulting
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
LVL 22

Expert Comment

ID: 20376880
FROM Calendar
WHERE dt >= '20070101'
 AND dt < '20080101'
FROM Reservations;
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
AND c.date BETWEEN 'startDate' AND 'endDate'
LVL 32

Accepted Solution

Brendt Hess earned 500 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).

Author Closing Comment

ID: 31411776
Works great!

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

719 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