Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Delete all rows more than 10 minutes old

Tags:

sql

sql-server

How can I delete All rows in SQL Server that were created later than 10 minutes. Thanks.

like image 880
StringBuilder Avatar asked Dec 25 '22 17:12

StringBuilder


2 Answers

Assuming that you have a column name Date_column which is storing the timestamp. You may try like this, where mi is the abbreviation for minute:

DELETE FROM Table_name
WHERE Date_column < DATEADD(mi,-10,GETDATE())
like image 146
Rahul Tripathi Avatar answered Dec 28 '22 06:12

Rahul Tripathi


SQL Server has no way to tell at what time the row was created. You will need to have an extra column in your table to log the time. If you do that, your delete statement becomes fairly straight forward.

like image 28
Pepe Avatar answered Dec 28 '22 05:12

Pepe