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
26 Answers
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 2You can use DateDiff for this. The where clause in your query would look like:
where DATEDIFF(day,pdate,GETDATE()) < 31 I dont know why all these complicated answers are on here but this is what I would do
where pdate >= CURRENT_TIMESTAMP -30 OR WHERE CAST(PDATE AS DATE) >= GETDATE() -30
This Should Work Fine
SELECT * FROM product WHERE pdate BETWEEN datetime('now', '-30 days') AND datetime('now', 'localtime') Below query is appropriate for the last 30 days records
Here, I have used a review table and review_date is a column from the review table
SELECT * FROM reviews WHERE DATE(review_date) >= DATE(NOW()) - INTERVAL 30 DAY You can use this to get the data of the last 30 days, based on a column.
WHERE DATEDIFF(dateColumn, CURRENT_TIMESTAMP) BETWEEN 0 AND 30