Check Whether A Date Lies Between Two Dates Using SQL


Hi

I have a given date, lets call it MyDate. I wish to check whether this date lies between two dates in a table using SQL like This:

"Select * From TableName Where MyDate Between CrStart And CrEnd".

TableName represents a Table of Bookings For A Conference Room. CrStart and CREnd Represent existing Bookings for a Specific conference room. If Mydate lies between CrStart and CREnd then the request should be rejected as "Conference Room Not Available".

I hope I am making myself clear. SQL does not like the Select Statement as it is.

Can somebody help me with this please.

Thanks.
NolancAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PrakashRaoBSConnect With a Mentor Commented:
Try this..

Select * From TableName Where convert(varchar,MyDate,101) Between convert(varchar,CrStart,101) And convert(varchar,CrEnd,101)
0
 
DhaestConnect With a Mentor Commented:
Include the start and end date:

Select * From TableName Where MyDate >= CrStart And MyDate <= CrEnd

Exclude the start and end date

Select * From TableName Where MyDate > CrStart And MyDate < CrEnd
0
 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
Your given statement is also correct statement, just check with your dates for the date format.
if the dates format for all the dates are not same then try to make it same and then use your statement.

"Select * From TableName Where MyDate Between CrStart And CrEnd"

regards,
Ram
0
 
bkokxConnect With a Mentor Commented:
When using dates in a SQL Select note that for instance Feb 1, 2010 as text becomes #02/01/2010#
0
 
NolancAuthor Commented:
Hi

Thanks for all your comntributions.
0
All Courses

From novice to tech pro — start learning today.