开发者

Get the 2 digit year in T-SQL

I need to get the current 2 digit year and increment by开发者_StackOverflow社区 one. So the current number I'm looking for should be 11. How?


You can do ( YEAR( GETDATE() ) % 100 ) + 1

See GETDATE & YEAR


This will work for you

select Right(Year(getDate())+ 1,2)


For SQL Server 2012 and above, I'd suggest using FORMAT(@DATE, 'yy'):

SELECT FORMAT(DATEADD(year, 1, GETDATE()), 'yy')

Format offers a cleaner, more readable solution. Thus, less guesswork, and a better maintainability.


SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 1),2) as YEAR


You can try this in sql server

SELECT FORMAT(GETDATE(), 'yy')


If you are always going to be using GetDate() why not just do something like this:

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!


select CAST( DAY(GETDATE()) as varchar(10))+'/'+CAST( month(GETDATE()) as varchar(10))+'/' +cast(right(year(getDate()),2) as varchar)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜