Solved

SQL query to delete table data greater than one year.

Posted on 2010-09-07
10
581 Views
Last Modified: 2012-05-10
Hi all!

I need to create a job where only one year of data is kept in a table for compliance.  So anything greater than a year has to be deleted.

Thanks!
kouts1
0
Comment
Question by:kouts1
10 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33621520
Use this as the date filter in the job query

Delete from mytable Where datecol < dateadd(y, -1, getdate())
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33621529

DELETE FROM table_name
WHERE DATEDIFF(year, date_column, getdate()) >= 1

Open in new window

0
 
LVL 3

Expert Comment

by:DanMerk
ID: 33621573
Please Note: While DATEDIFF is the better option, I would read the following about it:

http://www.beansoftware.com/T-SQL-FAQ/Subtract-DateTime-SmallDateTime.aspx

The bottom line is that it has a habit of rounding up, so a DATEDIFF in hours of 61 minutes will register as 2 hours. Therefore, if you need exact precision, you might want to use

DELETE FROM table_name
WHERE DATEDIFF(day, date_column, getdate()) >= 365
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33621610
I would stick to whole days, at least.

And you don't want to manipulate the table column.

So do something like this:

DELETE FROM tablename
WHERE dateColumn < DATEADD(YEAR, -1, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

The DATEADD(DAY ... DATEDIFF(DAY ... strip the time off GETDATE(), so that you delete data for whole days, not based on the time-of-day when the query is run.
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33622176
Just for performance I would suggest first define variable based on your definition of "1 year "
then use this variable in your delete query
You do need to have date column in your table that will define "age" of the row.

declare @dt datetime
set @dt = dateadd(year, -1,convert(varchar(10),getdate(),101))
select @dt
-- delete from myTable where dateCreated >=@dt
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33622409
SQL should be able to optimize a literal value **far better** than a variable.  So use a literal constant whenever you can.

Note that GETDATE() is considered a literal constrant, since SQL replaces it at the start of the batch, prior to creating the query plan.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33622511
While the discussion is refreshing, the first comment is already correct.

dateadd(y, -1, getdate())

will give you exactly 1 year prior to current time to the millisecond, including leap year calculation.
I don't believe stripping the time info is relevant, since this is an archival operation that is run every day so that extra op is moot.
0
 

Author Comment

by:kouts1
ID: 33626941
So this query should keep from current day back until one year?
dateadd(y, -1, getdate())

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33627135
yes

< dateadd(y, -1, getdate())
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33628659
>> I don't believe stripping the time info is relevant, since this is an archival operation that is run every day so that extra op is moot. <<

You're lucky, since your procedures seem never to fail :-) .

I would want a re-run of a failed proc to produce *exactly* the same results as the initial run if it had to be re-run later that day for any reaon.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now