Link to home
Start Free TrialLog in
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.
Avatar of Om Prakash
Om Prakash
Flag of India image

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?
Avatar of dwknight
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.
Avatar of 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
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
Avatar of pmcgivern
pmcgivern
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
select * from tabelname where date < date1 and date>date2
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