Search code examples
sql-serverdate-arithmetic

Delete rows with date older than 30 days with SQL Server query


I need a SQL statement to delete row that are older than 30 days.

My table events has a field date that contains the date and the time it was inserted in the database.

Will this work?
SELECT * from Results WHERE [Date] >= DATEADD(d, -30, getdate())


Solution

  • Use DATEADD in your WHERE clause:

    ...
    WHERE date < DATEADD(day, -30, GETDATE())
    

    You can also use abbreviation d or dd instead of day.