Solved

SQL query to delete table data greater than one year.

Posted on 2010-09-07
10
582 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
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: 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
 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

775 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