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:23
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 18:23
SELECT DateName(M, DateAdd(M, @MONTHNUMBER, -1))
查看更多
笑指拈花
4楼-- · 2018-12-31 18:24

i think this is enough to get month name when u have date.

SELECT DATENAME(month ,GETDATE())
查看更多
永恒的永恒
5楼-- · 2018-12-31 18:25

Here is my solution using some information from others to solve a problem.

datename(month,dateadd(month,datepart(month,Help_HelpMain.Ticket_Closed_Date),-1)) as monthname
查看更多
浪荡孟婆
6楼-- · 2018-12-31 18:26

in addition to original

SELECT DATENAME(m, str(2) + '/1/2011')

you can do this

SELECT DATENAME(m, str([column_name]) + '/1/2011')

this way you get names for all rows in a table. where [column_name] represents a integer column containing numeric value 1 through 12

2 represents any integer, by contact string i created a date where i can extract the month. '/1/2011' can be any date

if you want to do this with variable

DECLARE @integer int;

SET @integer = 6;

SELECT DATENAME(m, str(@integer) + '/1/2011')
查看更多
听够珍惜
7楼-- · 2018-12-31 18:26

Sure this will work

select datename(M,GETDATE())
查看更多
登录 后发表回答