문제

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