views:

135

answers:

3

how to get the 30 days before date from Todays Date. in Sql using Query

+3  A: 

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
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
Just add this to your answer:)
hgulyan
@hgulyan - will do!
amelvin
A: 

In DB2, you can use:

select current date - 30 days as some_time_ago from sysibm.sysdummy1;

You can actually do all sorts of wondrous datetime additions and subtractions such as:

current date + 2 years + 3 months + 10 days
current time + 4 hours - 6 minutes + 10 seconds
days(current date) - days(date('2010-01-01'))
paxdiablo
+1  A: 
SELECT TRUNC (SYSDATE - 30) FROM DUAL;

Executed today using ORACLE PL/SQL, it will produce:

April 14th, 2010 00:00:00

The chicken in the kitchen