Solved

determine if date ranges overlap

Posted on 2001-08-03
3
489 Views
Last Modified: 2011-10-03
I have a table that has a startdate and enddate field.  I want to create a function that accepts a start and end date parameter and compare these dates against the dates in the table to see if there are any overlapping days.  If there are overlapping days then I want the function to return False.

It shouldn't be hard but it's 5:00 on a Friday and my brain is mush.
0
Comment
Question by:jayh
3 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
Comment Utility
Here's one that should work (you'll have to convert it into a function - I don't have SQL2K to work with)

NOTE:  I recommend returning False for No Overlap - this allows this select to work, since Count would be 0:

SELECT Count(*) From MyTable
Where StartDate BETWEEN @Start And @END
   OR EndDate BETWEEN @Start And @End
   OR @Start Between StartDate AND EndDate


Given StartDate 5/1/2001   EndDate 5/15/2001

@Start 4/30/2001  @End 5/1/2001  Count = 1
@Start 5/15/2001  @End 5/17/2001  Count = 1
@Start 4/30/2001  @End 5/17/2001  Count = 1
@Start 5/16/2001  @End 5/17/2001  Count = 0
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
something like:

select *
from table1
where startdate between date1 and date2
or enddate between date1 and date2



date1 and date2 are the 2 fields in your table
startdate and enddate are the values given by the user
0
 

Author Comment

by:jayh
Comment Utility
Close enough...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now