Advertisement
Advertisement


Convert Month Number to Month Name Function in SQL


Question

I have months stored in SQL Server as 1,2,3,4,...12. I would like to display them as January,February etc. Is there a function in SQL Server like MonthName(1) = January? I am trying to avoid a CASE statement, if possible.

2012/05/23
1
210
5/23/2012 2:09:13 PM

Accepted Answer

A little hacky but should work:

SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))
159
10/9/2008 1:03:33 AM


SELECT DATENAME(month, GETDATE()) AS 'Month Name'
2011/11/21

SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
2011/11/21

Use the Best way

Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
2015/09/18

It is very simple.

select DATENAME(month, getdate())

output : January

2017/01/26

You can use the inbuilt CONVERT function

select CONVERT(varchar(3), Date, 100)  as Month from MyTable.

This will display first 3 characters of month (JAN,FEB etc..)

2011/11/21

Source: https://stackoverflow.com/questions/185520
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]