kjv kjv - 2 years ago 99
SQL Question

Delete items older than a day - SQL Server

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download