SQL Server:在日期的X天内返回记录

时间:2013-10-04 17:59:01

标签: sql-server

我在SQL Server中需要一个WHERE条件,我可以从给定日期返回过去7天的活动。

假装我有2列日期[dateA]和[dateB]

我正在寻找像

这样的东西
SELECT *
FROM TABLE
WHERE [dateB] >= ([dateA] - 7 days)

4 个答案:

答案 0 :(得分:2)

WHERE dateB >= DATEADD(DAY, -7, dateA)

Potentially useful reading ...

答案 1 :(得分:1)

select * from table
where dateB >= dateadd(dd,-7, dateA)

答案 2 :(得分:0)

SELECT * FROM TABLE WHERE [dateB])> = convert(datetime,[dateA] - 7)

你是如此亲密

答案 3 :(得分:0)

    IF you need past 7 days activity then you need take difference of date in that column from today i.e GETDATE() it will be something like this......

 WHERE (DATEDIFF(DAY, GETDATE(), dateA) <= 0 AND DATEDIFF(DAY, GETDATE(), dateA) > -7 ) 
        AND (DATEDIFF(DAY, GETDATE(), dateB) <= 0 AND DATEDIFF(DAY, GETDATE(), dateB) > -7 )