Reputation: 1017
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
Upvotes: 68
Views: 320043
Reputation: 85
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
Upvotes: 1
Reputation: 5600
You can use DateDiff
for this. The where clause in your query would look like:
where DATEDIFF(day,pdate,GETDATE()) < 31
Upvotes: 23
Reputation: 93694
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
Upvotes: 96
Reputation: 160
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
Upvotes: 8
Reputation: 510
This Should Work Fine
SELECT * FROM product
WHERE pdate BETWEEN datetime('now', '-30 days') AND datetime('now', 'localtime')
Upvotes: 4
Reputation: 388
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
Upvotes: 15