0Day Forums
Convert Month Number to Month Name Function in SQL - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Database (https://0day.red/Forum-Database)
+---- Forum: Microsoft SQL Server (https://0day.red/Forum-Microsoft-SQL-Server)
+---- Thread: Convert Month Number to Month Name Function in SQL (/Thread-Convert-Month-Number-to-Month-Name-Function-in-SQL)

Pages: 1 2 3


Convert Month Number to Month Name Function in SQL - recs811573 - 07-31-2023

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.


RE: Convert Month Number to Month Name Function in SQL - plainclothesman969 - 07-31-2023

A little hacky but should work:

SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))






RE: Convert Month Number to Month Name Function in SQL - chide885 - 07-31-2023

In some locales like Hebrew, there are [leap months][1] dependant upon the year so to avoid errors in such locales you might consider the following solution:

SELECT DATENAME(month, STR(YEAR(GETDATE()), 4) + REPLACE(STR(@month, 2), ' ', '0') + '01')


[1]:

[To see links please register here]




RE: Convert Month Number to Month Name Function in SQL - Propanjandra398 - 07-31-2023

This one worked for me:

@MetricMonthNumber (some number)

SELECT
(DateName( month , DateAdd( month , @MetricMonthNumber - 1 , '1900-01-01' ) )) AS MetricMonthName
FROM TableName

From a post above from @leoinfo and @Valentino Vranken. Just did a quick select and it works.





RE: Convert Month Number to Month Name Function in SQL - bus224656 - 07-31-2023

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



RE: Convert Month Number to Month Name Function in SQL - chervils638036 - 07-31-2023

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


RE: Convert Month Number to Month Name Function in SQL - competitor448723 - 07-31-2023

You can use the convert functin as below

CONVERT(VARCHAR(3), DATENAME(MM, GETDATE()), 100)


RE: Convert Month Number to Month Name Function in SQL - potentate390 - 07-31-2023

SELECT DATENAME(month, GETDATE()) AS 'Month Name'


RE: Convert Month Number to Month Name Function in SQL - lonai - 07-31-2023

Use this statement

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

this will convert the month number to month full string


RE: Convert Month Number to Month Name Function in SQL - unheritable176064 - 07-31-2023

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