views:

663

answers:

5

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

+3  A: 

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

Or something like this... ;)

KB22
+2  A: 
Delete <TableName>
Where DATEDIFF(day, <ColumnName>, getdate()) > 0

Raj

Raj
you might have forgot a 'FROM' ;)
KB22
From is not required with a delete statement :)
Raj
+1, my d'oh. ^^
KB22
+2  A: 

I generally advise against actually deleting data from your database because you never know when you may need to go back and recover or rollback to previous records because of data corruption or an audit, etc. Instead I would add an bit column title something like "IsDeleted" and set day old entries to true using an update statement.

Something like

'UPDATE tablename SET IsDeleted = 1 WHERE (DATEDIFF(day,DateCreatedOn,GETDATE()) > 0)

Where DateCreatedOn is where your record's created on or timestamp date would go

jlech
This is a tough generalization without knowing the problem set. There are as many good reasons to delete data as not. If you need to rollback or recover, that's why you have backups. If the standard operation of your app means you no longer need the data, then delete the data.
Joe
If you never delete data, eventually--inevitably--overcrowded tables will lead to degraded performance. If you must keep the data "hot", copying it to an archive table and then deleting from the source, perhaps after hours, should be a viable option.
Philip Kelley
I'll admit it's a bit over zealous, but it's better to have it and not need it, then not have it and need it. Imagine the kind of response you would get from a client who says they need all sales records from their e-Commerce application for the past 5 years and your response to them is "We deleted it because it was old and not currently needed by the application".Just because the application itself might not need the data, it doesn't mean that the client doesn't need it and can do without either.
jlech
An overlooked design criteria I picked up long ago is: for every fact (table, column, whatever), identify it's lifetime -- when can it be removed, deleted, forgotten. That way you'd know the *client* needs the data for five years. (Applications need performance, clients need data, and implementations need to support both.)
Philip Kelley
@Philip, I agree with your last statement, and looking over my answer again, I can see why you are stressing your point of view.All I wanted to get across with my answer is the "delete forever" notion this question brought up. Certainly I see no problem in moving "old" data from a production table to some sort of archive for increased performance. I just believe that no data should be physically deleted without some means of recovering it.
jlech
A: 

or

ts >= now() - INTERVAL 1 DAY

where ts is a name of the datetime column

Zepplock
And does his works on SQL Server?
gbn
Yes it does. At least on MySQL.
Zepplock
So please read documentation before -1 other people
Zepplock
Well, it doesn't work on SQL server...
Marien
A: 

When it comes to SQL, you have to specify what you mean by "older than a day".

  • DATEDIFF: it uses day boundary midnight so run it at 19th October 00:05 and you'll delete rows 6 minutes old (18th October 23:59)

  • 24 hours?

  • Yesterday midnight? Run code on 19th October, delete rows before 18th?

Also, don't put a function on a column.

This assumes 24 hours to the minute:

DELETE
    MyTableWhere
WHERE
    MyColumn < DATEADD(day, -1, GETDATE())

This assumes yesterday midnight:

DELETE
    MyTableWhere
WHERE
    MyColumn < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
gbn