Get last 30 day records from today date in SQL Server

user3224208 picture user3224208 · Dec 15, 2014 · Viewed 127.9k times · Source

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

Sample data:

Product:

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

I tried this query

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

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

Answer

Pரதீப் picture Pரதீப் · Dec 15, 2014

Add one more condition in where clause

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

Or use DateDiff

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