Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL query to delete table data greater than one year.

Posted on 2010-09-07
10
Medium Priority
?
593 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 70

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 70

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 2000 total points
ID: 33627135
yes

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

782 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