在 SQL Server 中获取从今天开始的最近 30 天的记录 [英] Get last 30 day records from today date in SQL Server

查看:109
本文介绍了在 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 

这篇关于在 SQL Server 中获取从今天开始的最近 30 天的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆