Solved

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

Posted on 2006-06-30
4
504 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

Industry Leaders: 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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

634 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