Convert Month Number to Month Name Function in SQL

前端 未结 30 1563
予麋鹿
予麋鹿 2020-11-22 11:53

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 a

30条回答
  •  清酒与你
    2020-11-22 12:33

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

提交回复
热议问题