在 SQL Server 中添加 +- 1 年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8533962/
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 13:40:03 来源:igfitidea点击:
add +- 1 year in SQL Server
提问by user1007103
I try to select the Products that has a yearmodel between +1 and -1 the current year. And I only want the Year (ex 2011) not the full date and time.
我尝试选择在当前年份具有 +1 和 -1 之间的年份模型的产品。我只想要年份(前 2011 年)而不是完整的日期和时间。
SELECT ProductName FROM tblProduct WHERE Year BETWEEN
year(getdate()+1) AND year(getdate()-1)
Does not work, but something similar maybe...
不起作用,但可能有类似的东西......
回答by Sebastian Piu
You are adding the 1 to getdate() so you are adding 1 day
您将 1 添加到 getdate() 所以您添加了 1 天
SELECT ProductName FROM tblProduct WHERE Year BETWEEN
(year(getdate()) -1) AND (year(getdate()) + 1)
回答by patjuh1978
SELECT dateadd(year, -1, getdate())
Would have done:
会做:
SELECT productname
FROM tblproduct
WHERE [year] between (select dateadd(year, -1, getdate()) AND (select dateadd(year, +1, getdate())