Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete items older than a day - SQL Server

Tags:

In a table in my datatase I have a datatime column which stores the time at which the record is added. How can I delete all records which are older than a day when I run a stored procedure (considering the current time) ?

like image 370
kjv Avatar asked Oct 19 '09 13:10

kjv


People also ask

How do you delete 10000 records in SQL?

If you need to remove 10 million rows and have 1 GB of log space available use Delete TOP(10000) From dbo. myTable (with your select clause) and keep running it till there are no more rows to delete.


1 Answers

You can build a DELETE statement making use of the datediff and the getdate functions.

Usage example:

DELETE FROM yourTable WHERE DATEDIFF(day,getdate(),thatColumn) < -1 
like image 101
KB22 Avatar answered Sep 17 '22 15:09

KB22