[英]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. 我正在使用SQL Server2012。我需要删除比去年同期更多的数据。
So far example, delete any records greater than 28/11/ 2015 . 到目前为止的示例,删除任何大于2015年 11 月 28日的记录。
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. 虽然这是滚动的,并且将成为每天运行的SP的一部分,所以它每天都会检查当前日期并删除。 What is the best way to do this?
做这个的最好方式是什么?
DELETE tblmytable
where MyDateField > GETDATE ()
How do I change to say > Today from last year? 我要怎么说>去年的今天?
Use dateadd 使用dateadd
DELETE
from tblmytable
where MyDateField > dateadd(yy, -1, GETDATE ())
Got it, this is the answer: 知道了,这就是答案:
Delete tblmytable
where MyDateField > DATEADD(year, -1, GETDATE())
You can try this: 您可以尝试以下方法:
delete from table
where dateField > dateadd(yy, -1, getdate())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.