How do you get the 30 days before today in SQL.
Asked
Active
Viewed 3e+01k times
91
-
5Which SQL DB do you use? MS SQL? MySQL? Oracle? – hgulyan May 14 '10 at 10:03
-
3edit: according to your previous question, it's obviously ms sql. – hgulyan May 14 '10 at 10:05
4 Answers
150
T-SQL
declare @thirtydaysago datetime
declare @now datetime
set @now = getdate()
set @thirtydaysago = dateadd(day,-30,@now)
select @now, @thirtydaysago
or more simply
select dateadd(day, -30, getdate())
MYSQL
SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)

amelvin
- 8,919
- 4
- 38
- 59
-
Just realised, this is written in T-Sql (Sql Server), if the answer is needed for MySql then something like: SELECT DATE_ADD(NOW(), INTERVAL -30 DAY) is the equivalent. – amelvin May 14 '10 at 10:05
-
1
-
19
In MS SQL Server, it is:
SELECT getdate() - 30;

Gaffi
- 4,307
- 8
- 43
- 73

Merin Nakarmi
- 3,148
- 3
- 35
- 42
-
1
-
1I think the only relevant difference in the original and the edited version is that the latter uses the `
` tag, which looks better. :)
– Sk8erPeter Aug 01 '13 at 10:21
4
SELECT (column name) FROM (table name) WHERE (column name) < DATEADD(Day,-30,GETDATE());
Example.
SELECT `name`, `phone`, `product` FROM `tbmMember` WHERE `dateofServicw` < (Day,-30,GETDATE());

Community
- 1
- 1

Ashley2605
- 41
- 4