Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Find if the date start/end lies in the range

Author  Topic 

azamsharp
Posting Yak Master

201 Posts

Posted - 2008-08-13 : 17:57:40
Hi,

I am looking to create a nice query that can tell me if the startdate,enddate spans a certain range. It should not be inside the range but it can be within the range. So, this means that startdate can be outside the STARTDATE in database but the enddate can be under the ENDDATE in the database. Here is my query and it works but I want a better query to handle this:

declare @startdate datetime
declare @enddate datetime

set @startdate = '10/01/2008'
set @enddate = '10/31/2008'

select * from mydatabase
where
start_dt between @startdate and @enddate OR
end_dt between @startdate and @enddate OR
@startdate between start_dt and end_dt OR
@enddate between start_dt and end_dt


Mohammad Azam
www.azamsharp.net

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-08-13 : 18:03:20
A better query? What is not good about the one you have now?


If you are just want to see if the ranges overlap, this will do what you want:
select * from mydatabase
where start_dt < @enddate and end_date > @startdate



CODO ERGO SUM
Go to Top of Page
   

- Advertisement -