90

How do you get the 30 days before today in SQL.

Mr. Radical
  • 1,833
  • 1
  • 18
  • 28
Innova
  • 4,591
  • 21
  • 75
  • 105

4 Answers4

143

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())

(DATEADD on BOL/MSDN)

MYSQL

SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)

( more DATE_ADD examples on ElectricToolbox.com)

amelvin
  • 8,819
  • 4
  • 37
  • 59
18

In MS SQL Server, it is:

SELECT getdate() - 30;

Gaffi
  • 4,255
  • 7
  • 44
  • 72
Merin Nakarmi
  • 2,851
  • 3
  • 31
  • 39
3
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
  • 31
  • 4
0

Try adding this to your where clause:

dateadd(day, -30, getdate())
jalsh
  • 751
  • 5
  • 18