SQL Query to find record above and below a specified date

dwknight
dwknight used Ask the Experts™
on
I have a record set that has the following record structure:

RecordID
BeginDate
EndDate
Details

I have a situation where a specified date (ie 16/03/2011) needs to be evaluated against the current recordset. The specified date will pretty much always occur between the records in the recordset  (ie - between the end date of record 1 and the start date of record 2)

The recordset will be made up of a number of records that are ordered by date range. The start date and end dates for each record will never overlap another record.

ie
Recordid       BeginDate       EndDate         Details
1                    04/01/2011     06/01/2011    Some Details
2                    10/01/2011    15/01/2011    Some Additional Detail
3                     20/01/2011    25/01/2011   Some Detail in Addition

I wanted a huge amount of help writing an SQL query that would be able to return the record before and after the specified date (16/01/2011). The date will be specified by user entry.

Thanks for the help in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
you can try something like:

SELECT * FROM Table_Name WHERE BeginDate <= @date_field and EndDate >= @date_field

-- where @date_field is parameter passed by user...
--->SQL query that would be able to return the record before and after the specified date (16/01/2011)
Is this statement correct?

Can you give more details on this with sample data, input and output?

Author

Commented:
Hey anillucky31, yes your query is correct - The record immediately before and immediately after the specified date are the required results. The data input is as specified in the question, and the result of the query will be displayed in a table with two rows to the screen.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Imran Javed ZiaConsultant Software Engineer - .NET Architect

Commented:
Hi,

You can use following Code

declare @dt as datetime

set @dt = '2011-03-16'

Select * From tbl
Where BeginDate<=@dt
Or EndDate >=@dt

Thanks
Use this statement it will work for you.

replace yourtable with actual table name
DECLARE @inputDate DATETIME

SET @inputDate = '01/16/2011'



SELECT * FROM YourTable WHERE @inputDate BETWEEN BeginDate AND EndDate
 
 union

SELECT * FROM (
SELECT TOP 1 * FROM YourTable  WHERE NOT EXISTS(SELECT NULL FROM YourTable  WHERE @inputDate BETWEEN BeginDate AND EndDate)
AND EndDate < @inputDate 
ORDER BY EndDate  DESC
) A
 UNION  
 
 
 
SELECT TOP 1 * FROM YourTable  WHERE NOT EXISTS(SELECT NULL FROM YourTable  WHERE @inputDate BETWEEN BeginDate AND EndDate)
AND BeginDate > @inputDate   
ORDER BY BeginDate  desc

Open in new window

If you only want the records either side of your date you could use this:

CREATE TABLE temp_test ( RecordId int, Begindate datetime, EndDate datetime, Details nvarchar(100))

INSERT INTO temp_test (RecordId, Begindate, EndDate, Details)
VALUES (1, '2011-01-04', '2011-01-06', 'Some Details')
INSERT INTO temp_test (RecordId, Begindate, EndDate, Details)
VALUES (2, '2011-01-10', '2011-01-15', 'Some Additional Detail')
INSERT INTO temp_test (RecordId, Begindate, EndDate, Details)
VALUES (3, '2011-01-20', '2011-01-25', 'Some Detail in Addition')

DECLARE @dt DATETIME
SET @dt = '2011-01-16'

SELECT * FROM (SELECT TOP 1 * FROM temp_test WHERE EndDate < @dt ORDER BY EndDate DESC) AS tbl1
UNION
SELECT * FROM (SELECT TOP 1 * FROM temp_test WHERE Begindate > @dt ORDER BY Begindate ASC) AS tbl2

select * from tabelname where date < date1 and date>date2

Author

Commented:
Thanks pmcgivern, your query was just what I needed!
i have one doubt if you pass SET @dt = '2011-01-14' in solution given by pmcgivern. What result should come?

in my understanding it should give this record VALUES (2, '2011-01-10', '2011-01-15', 'Some Additional Detail') as this date is existing between this data range.

You need only upper and lower row in case if inputted date is not in range.

Am i right? please correct me if i am wrong.

if you pass SET @dt = '2011-01-16' then this date is not in our tables ten in that case you want upper and lower date. but in case of SET @dt = '2011-01-14' it shoud give only one row as it is there in data.

please correct me

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial