Convert date to YYYYMM format
SELECT CONVERT(nvarchar(6), GETDATE(), 112)
SELECT LEFT(CONVERT(varchar, GetDate(),112),6)
I know it is an old topic, but If your SQL server
version is higher than 2012.
There is another simple option can choose, FORMAT function.
SELECT FORMAT(GetDate(),'yyyyMM')
sqlfiddle
Actually, this is the proper way to get what you want, unless you can use MS SQL 2014 (which finally enables custom format strings for date times).
To get yyyymm
instead of yyyym
, you can use this little trick:
select
right('0000' + cast(datepart(year, getdate()) as varchar(4)), 4)
+ right('00' + cast(datepart(month, getdate()) as varchar(2)), 2)
It's faster and more reliable than gettings parts of convert(..., 112)
.