在 SQL Server 中获取从今天开始的最近 30 天的记录

我有一个关于 SQL Server 的小问题:如何从这个表中获取最近 30 天的信息

I have small question about SQL Server: how to get last 30 days information from this table

样本数据:

产品:

Pdate
----------
2014-11-20
2014-12-12
2014-11-10
2014-12-13
2014-10-12
2014-11-15
2014-11-14
2014-11-16
2015-01-18

基于此表数据,我希望输出如下所示

Based on this table data i want output like below

pdate
-------
2014-11-20
2014-12-12
2014-12-13
2014-11-16

我试过这个查询

SELECT * 
FROM product 
WHERE pdate >= DATEADD(day, -30, getdate()).

但它现在给出了准确的结果.请告诉我如何在 SQL Server 中解决此问题

but it now give exactly result. Please tell me how to solve this issue in SQL Server

推荐答案

where子句中再添加一个条件

SELECT * FROM  product 
WHERE pdate >= DATEADD(day,-30,GETDATE()) 
and   pdate <= getdate()

或者使用DateDiff

SELECT * FROM  product 
WHERE DATEDIFF(day,pdate,GETDATE()) between 0 and 30 

相关文章