Convert date to YYYYMM format

SqlSql ServerTsql

Sql Problem Overview


I want to select value = 201301

select getdate(), cast(datepart(year, getdate()) as varchar(4))+cast(datepart(MONTH, getdate()) as varchar(2))

it returns 20131

what is the normal way to do this?

Sql Solutions


Solution 1 - Sql

SELECT CONVERT(nvarchar(6), GETDATE(), 112)

Solution 2 - Sql

SELECT LEFT(CONVERT(varchar, GetDate(),112),6)

Solution 3 - Sql

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

Solution 4 - Sql

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).

Solution 5 - Sql

A more efficient method, that uses integer math rather than strings/varchars, that will result in an int type rather than a string type is:

SELECT YYYYMM = (YEAR(GETDATE()) * 100) + MONTH(GETDATE())

Adds two zeros to the right side of the year and then adds the month to the added two zeros.

Solution 6 - Sql

You can convert your date in many formats, for example :

CONVERT(NVARCHAR(10), DATE_OF_DAY, 103) => 15/09/2016
CONVERT(NVARCHAR(10), DATE_OF_DAY, 3) => 15/09/16

Syntaxe :

CONVERT('TheTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
  • The code is an integer, here 3 is the third formating without century, if you want the century just change the code to 103.

In your case, i've just converted and restrict size by nvarchar(6) like this :

CONVERT(NVARCHAR(6), DATE_OF_DAY, 112) => 201609

See more at : http://www.w3schools.com/sql/func_convert.asp

Solution 7 - Sql

this is simple format

convert(varchar(6),getdate(),112)

Solution 8 - Sql

SELECT FORMAT(GETDATE(),'yyyyMM') as 'YYYYMM'

Solution 9 - Sql

It's month 1, so you're getting an expected value. you'll have to zeropad the month (1 -> 01), as per this answer: https://stackoverflow.com/questions/309165/how-do-i-convert-an-int-to-a-zero-padded-string-in-t-sql

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlovijiView Question on Stackoverflow
Solution 1 - SqlHamlet HakobyanView Answer on Stackoverflow
Solution 2 - SqlHoganView Answer on Stackoverflow
Solution 3 - SqlD-ShihView Answer on Stackoverflow
Solution 4 - SqlLuaanView Answer on Stackoverflow
Solution 5 - SqlBill StidhamView Answer on Stackoverflow
Solution 6 - SqlEma.HView Answer on Stackoverflow
Solution 7 - SqlkhairuddeenView Answer on Stackoverflow
Solution 8 - SqlFederer-57View Answer on Stackoverflow
Solution 9 - SqlMarc BView Answer on Stackoverflow