Friday, 31 January 2020

Display 12 months name from sql server


////////// Query

SELECT MONTH(DATEADD(MM, s.number, CONVERT(DATETIME, 0))) [ID], LEFT(DATENAME(MONTH, DATEADD(MM, s.number, CONVERT(DATETIME, 0))),3) [Value] 
FROM master.dbo.spt_values s WHERE [type] = 'P' AND s.number BETWEEN 0 AND 11 ORDER BY 1


////////////// Result


ID
Value
1
Jan
2
Feb
3
Mar
4
Apr
5
May
6
Jun
7
Jul
8
Aug
9
Sep
10
Oct
11
Nov
12
Dec


SELECT MONTH(DATEADD(MM, s.number, CONVERT(DATETIME, 0))) [ID], DATENAME(MONTH, DATEADD(MM, s.number, CONVERT(DATETIME, 0)))[Value]

FROM master.dbo.spt_values s WHERE [type] = 'P' AND s.number BETWEEN 0 AND 11 ORDER BY 1

//////////////  Result

ID
Value
1
January
2
February
3
March
4
April
5
May
6
June
7
July
8
August
9
September
10
October
11
November
12
December

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...