Convert Month Number to Month Name in SQL Server

One of my blog readers mailed me asking a simple way to convert a month number to month name. Here’s the simplest way in my opinion:

DECLARE @Mth smallint
SET
@Mth = 11
SELECT DateName(mm,DATEADD(mm,@Mth,-1)) as [MonthName]

OUTPUT

image

Similarly if you want to list all the month names for a year using a T-SQL statement, you can do this:

SELECT Number + 1 as [MonthNumber],
DateName(mm,DATEADD(mm,Number,0)) as [MonthName]
FROM master..spt_values
WHERE Type = 'P' and Number < 12

OUTPUT

image


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

5 comments:

Unknown said...

Another method


DECLARE @Mth smallint
SET @Mth = 11
select datename(month,'2000'+cast(@Mth as char(2))+'01')


Madhivanan

http://beyondrelational.com/blogs/madhivanan

Anonymous said...

I try like explanation above to get month name uset datename function and ok.
thank a lot for it.

see it on my sql tutorial

Anonymous said...

thanks it worked for me.

Unknown said...

Thanks a lot , that works perfect

Unknown said...
This comment has been removed by the author.