Solved

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

Posted on 2006-06-30
4
496 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
  • 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:ScottPletcher
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

758 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

19 Experts available now in Live!

Get 1:1 Help Now