Solved

determine if date ranges overlap

Posted on 2001-08-03
3
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
ID: 6350571
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 70

Expert Comment

by:Éric Moreau
ID: 6350573
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
ID: 6356117
Close enough...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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