Wednesday, June 29, 2016

SQL SERVER: How to get list of all month’s name

In this article I am going to explain how to get list of all month’s name in Sql server.


Description:
I want to display the list of all month’s name.

Implementation:

Run the below given query to generate the list of all (12) month’s name:

SELECT
 [Month Number] = number,
 [Month Name] =DATENAME(MONTH, '2016-' + CAST(number as varchar(2)) + '-1')
FROM master..spt_values
WHERE Type = 'P' and number between 1 and 12
ORDER BY Number

OR

Declare @Year VARCHAR(10)
SELECT @Year ='2016'
SELECT [Month Number] = number,[Month Name]= DATENAME(MONTH, cast(@year+'-'+ ltrim(rtrim(CAST(number as varchar(2)))) + '-1' as datetime))
FROM master..spt_values
WHERE Type = 'P' and number between 1 and 12
ORDER BY Number




No comments:

Post a Comment