61

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?

John Smith
  • 7,243
  • 6
  • 49
  • 61
loviji
  • 12,620
  • 17
  • 63
  • 94
  • For more information check this link it list different date format sql server supports . https://msdn.microsoft.com/en-us/library/ms187928.aspx – Yashveer Singh Jan 11 '17 at 16:37

10 Answers10

114
SELECT CONVERT(nvarchar(6), GETDATE(), 112)
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
67
SELECT LEFT(CONVERT(varchar, GetDate(),112),6)
Hogan
  • 69,564
  • 10
  • 76
  • 117
51

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

D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • 3
    Among all the answers this is easy to understand by just looking at it and also can be modified easily if needed. +1 up – nirav Jul 23 '18 at 07:13
  • 1
    FYI, the first argument of `FORMAT` is a `date` type, so passing in a `varchar` or `nvarchar` will result in an error like, `Argument data type nvarchar is invalid for argument 1 of format function.` – openwonk Oct 04 '19 at 18:42
  • Also, no matter how long the other solutions take, FORMAT with take at least 26 times longer even when other solutions seem to be much more complex. – Jeff Moden Dec 06 '22 at 19:43
4

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.

Bill Stidham
  • 1,460
  • 13
  • 8
2

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

Luaan
  • 62,244
  • 7
  • 97
  • 116
1

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

Ema.H
  • 2,862
  • 3
  • 28
  • 41
1

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

Federer-57
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – JayPeerachai May 25 '22 at 11:54
0

this is simple format

convert(varchar(6),getdate(),112)
JayPeerachai
  • 3,499
  • 3
  • 14
  • 29
0

To obtain a format modification for such type of value, in case u wanted to apply it for the whole column, u had to do the following:

SELECT TO_DATE(variable-which-format-u-wanna-change, 'YYYYMM') FROM table;

However, I don't know how to use the output to add a column u_u

c336837
  • 1
  • 1
  • This is an old question that has several good answers. If you need more info please post a new question. – ufosnowcat Dec 08 '22 at 16:04
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/33350235) – ahuemmer Dec 10 '22 at 08:22
-1

It's month 1, so you're getting an expected value. you'll have to zeropad the month (1 -> 01), as per this answer: How do I convert an int to a zero padded string in T-SQL?

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500