Solved

Query/SP to determine if date range overlaps record date ranges

Posted on 2006-06-30
4
503 Views
Last Modified: 2009-12-16
I'm looking for a SELECT statement or stored procedure that allows me to return records whose date range overlaps a specified date range.  Here is a sample table:

cid        course_name      date_start      date_end     description
=========================================
1          Intro to Stuff       5/30/2006      7/2/2006     null
2          Ethics                 1/2/2006       1/31/2006    null
3          Physics I             2/20/2006     7/1/2006      null
4          Adv. Algebra       2/23/2006     4/23/2006    null
5          Accounting 101    1/27/2006     6/6/2006     null

Let's say I give the user the following input:
Date Start: ____________
Date End: ____________

Let's say the user enters a start date of 1/25/2006 and an end date of 2/22/2006.  I want to return all records that have dates have start/end dates that overlap the user input, 1/25/2006-2/22/2006, which in this example would be records 2, 3, and 5.

Does anyone understand what I'm looking for?  Can you come up with a solution?
0
Comment
Question by:hendridm
[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
  • 2
4 Comments
 
LVL 13

Assisted Solution

by:devsolns
devsolns earned 175 total points
ID: 17019835
DECLARE @StartDt datetime
DECLARE @EndDt datetime

SET @StartDt = '1/25/2006 12:00:00 AM'
SET @EndDt = '2/22/2006 12:00:00 AM'

SELECT * FROM MyTable WHERE ((date_start between @StartDt AND @EndDt) OR (date_end between @StartDt AND @EndDt))
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 325 total points
ID: 17019922
devsolns--

What about when the start and end dates extend beyond the entered date range in both directions?

SELECT * FROM MyTable WHERE ((date_start between @StartDt AND @EndDt) OR (date_end between @StartDt AND @EndDt) OR ((date_start < @StartDt) AND (date_end > @EndDt)))

0
 
LVL 13

Expert Comment

by:devsolns
ID: 17019951
Yes......correct jmundsack i should have added that as well.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 17020383
SELECT *
FROM tableName
WHERE NOT (@dateStart > date_end OR @dateEnd < date_start)


That is, if the desired start date is after the table end date or the desired end date is before the table start start date, then the row should NOT be selected; otherwise it should be.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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 ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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