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 2000 Forums
 SQL Server Development (2000)
 Comparing DateTime Ranges

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-09 : 09:35:00
Jack writes "I have a startDate and endDate in my database. I want to determine if a supplied range (requestedStart and requestedEnd) conflicts.

If I have startDate = 1/1/2006 08:00 AM and endDate = 1/1/2006 05:00 PM in the database how do I test if 1/1/2006 7:00 AM and 1/1/2006 06:00PM conflict?

Thanks."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-09 : 09:52:48
Where yourDate not between StartDate and EndDate

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-08-09 : 09:53:40
If you're looking for any overlap, this should do it:
SELECT mt.startDate, mt.endDate
FROM MyTable AS mt
WHERE @requestedEnd Between mt.startDate AND mt.endDate OR @requestedStart Between mt.startDate AND mt.endDate


Mark
Go to Top of Page

airjrdn
Starting Member

35 Posts

Posted - 2006-08-09 : 09:56:00
declare @requestedStart datetime,
@requestedEnd datetime

select @requestedStart = convert(datetime, '1/1/2006 7:00 AM'),
@requestedEnd = convert(datetime, '1/1/2006 6:00 AM')

if datediff(s, @requestedStart, @requestedEnd) < 0
select 'conflict'
else
select 'ok'
Go to Top of Page
   

- Advertisement -