Convert Month Number to Month Name Function in SQL

2018-12-31 17:38发布

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.

30条回答
无与为乐者.
2楼-- · 2018-12-31 18:27

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..)

查看更多
听够珍惜
3楼-- · 2018-12-31 18:28

Use this statement

SELECT TO_CHAR(current_date,'dd MONTH yyyy') FROM dual

this will convert the month number to month full string

查看更多
像晚风撩人
4楼-- · 2018-12-31 18:30

Just subtract the current month from today's date, then add back your month number. Then use the datename function to give the full name all in 1 line.

print datename(month,dateadd(month,-month(getdate()) + 9,getdate()))
查看更多
荒废的爱情
5楼-- · 2018-12-31 18:30

To convert month number to month name, try the below

declare @month smallint = 1
select DateName(mm,DATEADD(mm,@month - 1,0))
查看更多
不流泪的眼
6楼-- · 2018-12-31 18:33
select monthname(curdate());

OR

select monthname('2013-12-12');
查看更多
牵手、夕阳
7楼-- · 2018-12-31 18:33
SELECT DATENAME(MONTH,dateadd(month, -3,getdate()))
查看更多
登录 后发表回答