sqlsql-serverdatetime

How to get the 30 days before date from today's date


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


Solution

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