SQL 如何在SQL中添加迄今为止的年份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21554717/
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 00:58:31  来源:igfitidea点击:

How to add years to date in SQL?

sqlsql-server

提问by Srinivas

How to add 2 years to date in SQL.

如何在 SQL 中添加 2 年至今。

select left(cast(D as datetime) ,11)  from table

In the above query how can i add 2 years.

在上面的查询中,我如何添加 2 年。

Output is,

输出是,

Jan  1 2012
Jan  2 2012
Jan  3 2012
Jan  4 2012

but i want output as,

但我想要输出,

Jan  1 2014
Jan  2 2014
Jan  3 2014
Jan  4 2014

Thank you

谢谢

回答by gvee

DateAdd()is the function you want http://technet.microsoft.com/en-us/library/ms186819.aspx

DateAdd()是你想要的功能http://technet.microsoft.com/en-us/library/ms186819.aspx

SELECT DateAdd(yy, 2, Cast(d As datetime))

回答by Punter015

select dateadd(yy,2,getdate())

回答by Srinivas

finally i got, Query is SELECT left(DateAdd(yy, 2, Cast(d As datetime)),11) from table

最后我得到了,查询是 SELECT left(DateAdd(yy, 2, Cast(d As datetime)),11) from table

回答by SQLMason

You canuse convert instead of the left:

可以使用 convert 而不是 left:

SELECT CONVERT(VARCHAR, DATEADD(year, 2, CAST(d AS DATETIME)), 107) FROM table