Question

I have a table with 2 dates.

Date A
Date B

I need to select all the rows where date B happened WITHIN 48 hours after date A.

Sample Data

Date A          Date B
1/28/2011   1/19/2014
12/21/2010  1/18/2014
2/5/2014    2/7/2014

In this case the 3rd row qualifies

Was it helpful?

Solution

DATEADD() should do the trick. SELECT DATEADD(hh, 48, getdate()) returns 48 hours from now.

In your example you could replace getdate() with Date A, somewhere along the lines of:

SELECT DateB
FROM   x
WHERE  DateB BETWEEN DateA AND DATEADD(hh, 48, DateA)

OTHER TIPS

SELECT DateA, DateB
FROM dbo.TableName
WHERE DATEDIFF(hour, DateA, DateB) <= 48

Sql-Fiddle

Use ABS if it's not important which date is earlier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top