Solved

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 VS Server 2016 for SQL Cluster 4 40
Help Required 3 92
SQL View nearest date 5 35
Tsql query 6 20
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

785 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