Solved

determine if date ranges overlap

Posted on 2001-08-03
3
509 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

691 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