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.
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.
--->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?
Is this statement correct?
Can you give more details on this with sample data, input and output?
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.
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select * from tabelname where date < date1 and date>date2
ASKER
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
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
SELECT * FROM Table_Name WHERE BeginDate <= @date_field and EndDate >= @date_field
-- where @date_field is parameter passed by user...