SQL 如何选择小于 1 年的记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3652099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:26:38  来源:igfitidea点击:

How do I select records less than 1 year old?

sqlsql-servertsql

提问by Mike Sav

This should be a fairly simple question but I know very little about SQL. I have a database which has the following fields:

这应该是一个相当简单的问题,但我对 SQL 知之甚少。我有一个包含以下字段的数据库:

  • client_id
  • scheduled_amount
  • deposit_amount
  • message_code_id
  • note_text
  • system_date
  • client_id
  • scheduled_amount
  • deposit_amount
  • message_code_id
  • note_text
  • system_date

Now I wish to select all the records that are less than 1 year old from when the SQL statement is run. I know I should use DateDiff, anyone got any ideas?

现在我希望从运行 SQL 语句时选择所有小于 1 年的记录。我知道我应该使用DateDiff,有人有任何想法吗?

Thanks

谢谢

回答by RedFilter

select *
from MyTable
where MyDate > DATEADD(year, -1, GetDate())

回答by STEVE

TRY

尝试

SELECT * FROM TABLE_NAME WHERE SYSTEM_DATE > DATEADD(YYYY, -1, GETDATE())

(UNTESTED)

(未经测试)

回答by STEVE

select ... from ... where YEAR(GETDATE()) - YEAR(YOURDATE) = 1