Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 automating record deletion

Author  Topic 

mr_dayal
Starting Member

37 Posts

Posted - 2008-07-25 : 07:36:04
I have a table with following data

Item Id Date

Power Kiting(Foll Kite) 24/07/2008 7:21:21 PM
IKO Kiteboarder Level III 24/07/2008 7:40:22 PM
Power Kiting(Foll Kite) 25/07/2008 3:55:04 PM
IKO Kiteboarder Level I 25/07/2008 3:57:23 PM
IKO Kiteboarder Level I 24/07/2008 7:31:08 PM
IKO Kiteboarder Level II 24/07/2008 7:34:49 PM



Now this database is made in sql express edition and is used for a web site..
I want to delete those records whose date filed is less then one day ago...(records which r older then 24 hours)..



Mr Dayal

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-25 : 07:40:39
You can create a sql agent job which fires the delete statement to delete records older than 24 hrs. then schedule the job to be run as per your convenient schedule.
the delete statement will be like

DELETE FROM YourTable WHERE DateField < DATEADD(d,DATEDIFF(d,0,GETDATE()),-1)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-07-25 : 11:53:56
quote:
Originally posted by visakh16


DELETE FROM YourTable WHERE DateField < DATEADD(d,DATEDIFF(d,0,GETDATE()),-1)


Just to be clear, that will delte all records from the previous day. If you want a rolling 24 hour window then you probably should just subtract 24 hours from the current datetime: [CODE]DELETE FROM YourTable WHERE DateField < DATEADD(HOUR, -24, GETDATE())[/CODE]
Go to Top of Page
   

- Advertisement -