简体   繁体   English

SQL Server Delete记录大于去年的这个时间

[英]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.

相关问题 SQL Server:查找大于 5 的最近连续记录 - SQL Server : find recent consecutive records that are greater than 5 SQL 服务器查询查找最后出现的大于 0 的数字。如果没有大于 0 的数字,则查询应返回 0 - SQL Server query to find last occurred number greater than 0. If there is no number greater than 0 then query should return 0 在SQL Server中删除时间范围内的记录 - delete records in time ranges in SQL Server SQL 查询两个日期之差大于某个值时删除记录 - SQL query to delete records when the difference between two dates is greater than a certain value 使用 sql 删除记录,如果它大于某个数字并附加一个单位(例如 3M 或 3cm) - Delete records using sql ,if it is greater than certain number with an unit attached to it (eg 3M or 3cm) 大于上次从数据库登录的时间 - Greater than last login time from db SQL Server删除记录 - SQL Server delete records 删除表中所有小于去年当前日期的记录,但不删除过去 2 年给定月份的结束日期 - Delete all records in the table that are less than the last year's current date, but don't delete the end dates for the given months for past 2 years 如果时间大于4小时,则从数据库中删除 - Delete from database if time is greater than 4 hours SQL 返回同一年的最后 30 条记录 - SQL return last 30 records on the same year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM