Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slecting records within a date range...

Status
Not open for further replies.

psymonj

Programmer
Mar 31, 2005
49
GB
I apologise in advance for not having found a solution that may already exist on this site (if that is the case, I'd be very grateful if I could be pointed to the thread...).

I need to perform a query on a small table (bookings). The table has 2 fields that include the booked from and to dates.
I'd like to be able to run a query to see if an individual date is 'taken'.

The format of the 2 fields is YYYY-MM-DD, the date passed to the query is currently formatted identically, but could easily be changed if need be.

I've tried many other work-arounds but am very frustrated now.
Basically, I'd like a true if 2008-10-22 is given (assuming that there is a book_from of 2008-10-19 and a book_to of 2008-10-26).

Many thanks in advance!

Simon Clements-Hawes
 
Are the fields of type date or varchar?

Code:
SELECT *FROM dates where (fromdate <= 'datetotest' AND todate >='datetotest')

If you get any rows that means its taken. If not, then the date is free.

If the fields are var char, then you'll need to cast them to dates.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi - now I look back on it (after several coffees), I'm thinking that my idea of a query might be quite impossible.

Pseudo of what I have:
while (day <= month_num_days)
{
do my query;
count query results;
if results >=1 {set *css_dark*;}
else {set *css_lite*;}
print "<td class=*css*>$day_num</td>";
}

Fields with the booked dates are DATE format in MySQL table.

Simon Clements-Hawes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top