返回给定日期内的所有记录

时间:2015-03-16 21:50:47

标签: sql-server

问题

如何将所有记录从今天返回到将来的特定天数?

伪代码中的示例

Select every record from table 
where plandate = today and the next 14 days

我尝试了什么?

select *
from joblist
WHERE  plandate >= GETDATE() +14 

2 个答案:

答案 0 :(得分:2)

您可以这样做:(我省略声明)

set @today = GETDATE()
set @endDate = DATEADD(DAY,15,GETDATE())

SELECT your_Records from TABLE
WHERE plandate >= @today
AND plandate < @endDate

您也可以使用BETWEEN子句:

SELECT your_Records from TABLE
WHERE plandate BETWEEN @today AND @endDate

答案 1 :(得分:1)

您可以使用DATEADD

WHERE  plandate >= CAST(GETDATE() AS DATE)
AND    plandate < DATEADD(day,+15, CAST(GETDATE() AS DATE))

请注意,只有在至少使用SQL-Server 2008时才能拥有Date type