Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

947 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

20 Experts available now in Live!

Get 1:1 Help Now