Delete rows with date older than 30 days with SQL Server query
Asked Answered
S

9

84

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

Subdual answered 6/12, 2010 at 9:30 Comment(5)
Actually.... Deletes "fields"?? You delete rows, not fields. Do you want to NULL the fields?Gomulka
Your SQL above will select rows where days is within the last 30 days.Gomulka
Does this answer your question? how to get the 30 days before date from Todays DateSheff
In reality, this is a typo question where you basically got the comparison symbols going in the wrong direction.Sheff
@ColinMackay nulling fields might actually be a legitimate thing to do when, for example, anonymizing data.Impassive
G
156

Use DATEADD in your WHERE clause:

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

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

Gomulka answered 6/12, 2010 at 9:32 Comment(5)
If you require 1 month instead of 30 days on average for month, than you can modify it to DATEADD(MONTH, -5, GETDATE())Monkery
FUNCTION fu_dev.DATEADD does not exist. Why am i getting this error? fu_dev is my database.Postilion
@Monkery Just to be clear it should be DATEADD(MONTH, -1, GETDATE()), using -5 would take off 5 months not 1.Beehive
@Colin; Amazing, I was able to quickly fix my query. But in a case where you deal with more than 3 dates in a query, can I still make use of this statement?Dicho
@Dicho This is just one clause, you can filter on as many dates as you need by joining the clauses together with AND or OR.Gomulka
S
18

You could also use

SELECT * from Results WHERE date < NOW() - INTERVAL 30 DAY;
Shope answered 21/9, 2017 at 8:20 Comment(1)
Good solution, but be careful using NOW() in your other queries, it won't cache it in furtherCombs
A
8

Although the DATEADD is probably the most transparrent way of doing this, it is worth noting that simply getdate()-30 will also suffice.

Also, are you looking for 30 days from now, i.e. including hours, minutes, seconds, etc? Or 30 days from midnight today (e.g. 12/06/2010 00:00:00.000). In which case, you might consider:

SELECT * 
FROM Results 
WHERE convert(varchar(8), [Date], 112) >= convert(varchar(8), getdate(), 112)
Ammann answered 6/12, 2010 at 9:44 Comment(0)
I
8

To delete records from a table that have a datetime value in Date_column older than 30 days use this query:

USE Database_name;
DELETE FROM Table_name
WHERE Date_column < GETDATE() - 30

...or this:

USE Database_name;
DELETE FROM Table_name
WHERE Date_column < DATEADD(dd,-30,GETDATE())

To delete records from a table that have a datetime value in Date_column older than 12 hours:

USE Database_name;
DELETE FROM Table_name
WHERE Date_column < DATEADD(hh,-12,GETDATE())

To delete records from a table that have a datetime value in Date_column older than 15 minutes:

USE Database_name;
DELETE FROM Table_name
WHERE Date_column < DATEADD(mi,-15,GETDATE())

From: http://zarez.net/?p=542

Illuviation answered 7/2, 2018 at 17:6 Comment(2)
The technique of using DATEADD(GETDATE()) was a well-worn path on this page long before you posted. Your answer only contains redundant advice or if there is new advice it does not answer the posted question. In other words, the new parts are useless parts in respect to the posted question. Imagine if everyone did this when they wanted to post an answer -- they duplicate an earlier posted answer, then stretch the scope of the page to suit their own answer ...we would not have a good, clean, focused resource for researchers.Sheff
Time of posting is irrelevant to good content curation on Stack Overflow.Sheff
M
3

GETDATE() didn't work for me using mySQL 8

ERROR 1305 (42000): FUNCTION mydatabase.GETDATE does not exist

but this does:

DELETE FROM table_name WHERE date_column < CURRENT_DATE - 30;
Misjoinder answered 9/12, 2019 at 3:23 Comment(2)
As per the tag on the question, the question relates to SQL Server, not mySQL.Gomulka
It will also work for PostgresSQL.Misjoinder
B
1

You could also set between two dates:

Delete From tblAudit
WHERE Date_dat < DATEADD(day, -360, GETDATE())
GO
Delete From tblAudit
WHERE Date_dat > DATEADD(day, -60, GETDATE())
GO
Bircher answered 11/9, 2012 at 6:35 Comment(0)
S
1

We can use this:

    DELETE FROM table_name WHERE date_column < 
           CAST(CONVERT(char(8), (DATEADD(day,-30,GETDATE())), 112) AS datetime)

But a better option is to use:

DELETE FROM table_name WHERE DATEDIFF(dd, date_column, GETDATE()) > 30

The former is not sargable (i.e. functions on the right side of the expression so it can’t use index) and takes 30 seconds, the latter is sargable and takes less than a second.

Spermatocyte answered 19/9, 2018 at 12:7 Comment(0)
C
0

Instead of converting to varchar to get just the day (convert(varchar(8), [Date], 112)), I prefer keeping it a datetime field and making it only the date (without the time).

SELECT * FROM Results 
WHERE CONVERT(date, [Date]) >= CONVERT(date, GETDATE())
Cressi answered 17/2, 2017 at 14:41 Comment(0)
A
0

Delete row older than 30 days.

SELECT * FROM TABLE_NAME where timestampString <= now() - interval 30 DAY;
Arthrospore answered 14/2, 2022 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.