Michael
Michael

Reputation: 2657

SQL Server Delete records greater than this time last year

I'm using SQL Server 2012. I need to delete data that is greater than this time last year.

So far example, delete any records greater than 28/11/2015.

This is rolling though and will be part of a SP that will run each day, so everyday it checks the current date and deletes. What is the best way to do this?

DELETE tblmytable
where MyDateField > GETDATE ()

How do I change to say > Today from last year?

Upvotes: 0

Views: 312

Answers (3)

purva
purva

Reputation: 1

You can try this:

delete from table
where dateField > dateadd(yy, -1, getdate())

Upvotes: -2

Michael
Michael

Reputation: 2657

Got it, this is the answer:

Delete tblmytable
where MyDateField > DATEADD(year, -1, GETDATE())

Upvotes: 0

JohnHC
JohnHC

Reputation: 11205

Use dateadd

DELETE 
from tblmytable
where MyDateField > dateadd(yy, -1, GETDATE ())

Upvotes: 5

Related Questions