How do you get the 30 days before today in SQL.
4 Answers
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)
-
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.– amelvinMay 14, 2010 at 10:05
-
1
-
In MS SQL Server, it is:
SELECT getdate() - 30;
-
1What is the difference Gaffi? Why you think edit was necessary? Aug 23, 2012 at 16:38
-
1I think the only relevant difference in the original and the edited version is that the latter uses the
<code>
tag, which looks better. :) Aug 1, 2013 at 10:21
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());