November 16, 2011 at 7:22 am
Hi,
Does anyone have a script where I can delete data from a specified table, leaving just 1-2months behind?
Thanks
November 16, 2011 at 7:25 am
DELETE FROM dbo.tbl
WHERE dt >= DATEADD(M, -2, GETDATE())
November 16, 2011 at 7:33 am
Hi thanks.
I am assuming if I change the -2 to -1 it will be leaving just 1 month of data behind?
November 16, 2011 at 7:39 am
Yes.
Now you need to define what months means in this case.
Change the where to a select to see exactly what it does and adjust accordingly.
November 16, 2011 at 7:41 am
Try it out and see what values you are using.
select DATEADD(M, -2, GETDATE()), DATEADD(M, -1, GETDATE())
Remember that if you don't understand the code you are running you could be running something that you really don't want to. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 16, 2011 at 7:56 am
Thanks again. I shall have a little play around.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply