Avatar of dwknight
dwknight
 asked on

SQL Query to find record above and below a specified date

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.
Web BrowsersMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
anillucky31

8/22/2022 - Mon
Om Prakash

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...
anillucky31

--->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?
dwknight

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Imran Javed Zia

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
anillucky31

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

ASKER CERTIFIED SOLUTION
pmcgivern

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ramesh Babu Vavilla

select * from tabelname where date < date1 and date>date2
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dwknight

ASKER
Thanks pmcgivern, your query was just what I needed!
anillucky31

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