How can we get the period from getdate() function in sql?
I tried below
SELECT CONVERT(VARCHAR(12), GETDATE(), 106)
but this gives the result in dd mmm yyyy format.
I want the result to be like 202009 (YYYYmm)
Asked
Active
Viewed 254 times
-2

Vahid Farahmandian
- 6,081
- 7
- 42
- 62

Sugandha Sharma
- 55
- 1
- 1
- 6
2 Answers
0
Never mind. I found it Its
SELECT LEFT(CONVERT(varchar, GetDate(),112),6)

Vahid Farahmandian
- 6,081
- 7
- 42
- 62

Sugandha Sharma
- 55
- 1
- 1
- 6
-
You should always set the length for `N/VARCHAR(n)` – Ilyes Sep 09 '20 at 08:32
0
You can try these:
SELECT FORMAT(GETDATE(),'yyyyMM');
Or
SELECT CONVERT(CHAR(6), GETDATE(), 112);

Vahid Farahmandian
- 6,081
- 7
- 42
- 62