Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the 30 days before date from Todays Date

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

like image 716
Innova Avatar asked May 14 '10 09:05

Innova


People also ask

How to get 30 days prior to the current date in JavaScript?

To get the date that is 30 days prior to the current date with JavaScript, we can call the setDate method with the current date subtracted by 30 to get the date 30 days before the current date.


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

(DATEADD on BOL/MSDN)

MYSQL

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

( more DATE_ADD examples on ElectricToolbox.com)

like image 84
amelvin Avatar answered Sep 30 '22 10:09

amelvin


In MS SQL Server, it is:

SELECT getdate() - 30;

like image 44
Merin Nakarmi Avatar answered Sep 30 '22 10:09

Merin Nakarmi