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

有帮助吗?

解决方案

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)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top