Solved

Check Whether A Date Lies Between Two Dates Using SQL

Posted on 2010-08-16
5
805 Views
Last Modified: 2013-11-26

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.
0
Comment
Question by:Nolanc
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 30 total points
ID: 33443831
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
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 30 total points
ID: 33443984
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
 
LVL 3

Assisted Solution

by:bkokx
bkokx earned 30 total points
ID: 33444280
When using dates in a SQL Select note that for instance Feb 1, 2010 as text becomes #02/01/2010#
0
 
LVL 3

Accepted Solution

by:
PrakashRaoBS earned 35 total points
ID: 33444496
Try this..

Select * From TableName Where convert(varchar,MyDate,101) Between convert(varchar,CrStart,101) And convert(varchar,CrEnd,101)
0
 

Author Closing Comment

by:Nolanc
ID: 33445546
Hi

Thanks for all your comntributions.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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