If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Return Dates in a Range

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-10, 12:54
wowdezign wowdezign is offline
Registered User
 
Join Date: Dec 2009
Posts: 7
Return Dates in a Range

I am trying to return a list of the dates that are found in date ranges in a result set.

For example, I have a reservations table that has a res_start column and a res_end column. I want to run a query that returns all the dates that fall into any of the ranges returned. Is this possible?

Important: I don't want to return a range, I want all the individual dates that fall in any of the ranges. I am using the dates in an availability table.

Thanks for any direction you can give.
Reply With Quote
  #2 (permalink)  
Old 01-05-10, 13:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
see this thread and create a numbers table with as many numbers as the longest date range

then change the query as follows --
Code:
SELECT r.res_start + INTERVAL n DAY AS my_date
  FROM reservations AS r
CROSS
  JOIN numbers
 WHERE r.somekey = somevalue -- THIS LINE IS IMPORTANT
   AND r.res_start + INTERVAL n DAY <= r.res_end
the line marked important is where you select which reservation you want to generate the dates for

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-06-10, 04:01
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
...and from your numbers table you can create a calendar table too
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On