Solved

SQL query to delete table data greater than one year.

Posted on 2010-09-07
10
580 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 74

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:ScottPletcher
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

19 Experts available now in Live!

Get 1:1 Help Now