获取 T-SQL 中的 2 位数年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3708084/
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
Get the 2 digit year in T-SQL
提问by fearofawhackplanet
I need to get the current 2 digit year and increment by one. So the current number I'm looking for should be 11.
我需要获取当前的 2 位数年份并加一。所以我正在寻找的当前数字应该是 11。
Probably really simple but I'm a sql noob :)
可能真的很简单,但我是一个 sql noob :)
Thanks
谢谢
回答by Jon Freedman
回答by Pranay Rana
This will work for you
这对你有用
select Right(Year(getDate())+ 1,2)
回答by Jonathan
SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 1),2) as YEAR
回答by wp78de
For SQL Server 2012 and above, I'd suggest using FORMAT(@DATE, 'yy')
:
对于 SQL Server 2012 及更高版本,我建议使用FORMAT(@DATE, 'yy')
:
SELECT FORMAT(DATEADD(year, 1, GETDATE()), 'yy')
Format
offers a cleaner, more readable solution. Thus, less guesswork, and a better maintainability.
Format
提供了一个更清晰、更易读的解决方案。因此,更少的猜测和更好的可维护性。
回答by Juan Cabrera
If you are always going to be using GetDate() why not just do something like this:
如果您总是要使用 GetDate() 为什么不这样做:
Select (Year(GetDate()) - 2000) + 1
Dang people. Always making things so complicated. It's not like you are going to be living for another 1000 years!
党人。总是把事情弄得这么复杂。你不会再活1000年吧!
回答by Rajesh Maurya
select CAST( DAY(GETDATE()) as varchar(10))+'/'+CAST( month(GETDATE()) as varchar(10))+'/' +cast(right(year(getDate()),2) as varchar)