Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL MONTHNAME() from numbers

Tags:

mysql

Is there anyway to get the MONTHNAME() from just the number of the month (1-12)? For example if I have 6,7,8 is there any native way in MySQL to transform those into June,July,August?

like image 916
Hunter McMillen Avatar asked Aug 11 '11 13:08

Hunter McMillen


People also ask

How to convert month number to month name in SQL?

In SQL SERVER, we can use a combination of functions 'DATENAME' and 'DATEADD' functions to get a month name from a month number. Oracle: In Oracle, we can use a combination of functions 'TO_CHAR' and 'TO_DATE' functions to get a month name from a month number.

How to get date from month name in SQL?

MySQL MONTHNAME() Function The MONTHNAME() function returns the name of the month for a given date.

How many parameters does Monthname () function requires explain with example?

MONTHNAME() Function in MySQL It Returns 0 when MONTH part for the date is 0 or greater than 12 otherwise it returns month name between January to December. Parameter : This method accepts one parameter as mentioned above and described below : date : The date or datetime from which we want to extract the month name.


3 Answers

You can use STR_TO_DATE() to convert the number to a date, and then back with MONTHNAME()

SELECT MONTHNAME(STR_TO_DATE(6, '%m'));  +---------------------------------+ | MONTHNAME(STR_TO_DATE(6, '%m')) | +---------------------------------+ | June                            | +---------------------------------+ 

Warning: This could be slow if done over a lot of rows.

like image 142
Michael Berkowski Avatar answered Sep 26 '22 03:09

Michael Berkowski


A somewhat ugly way would be SELECT MONTHNAME(CONCAT('2011-',8,'-01'));

like image 42
Mchl Avatar answered Sep 25 '22 03:09

Mchl


Before reading Michael's great answer I had thought something like this

select elt(3,'January','February','March',....)

but his one is much better. :)

like image 27
Nicola Cossu Avatar answered Sep 25 '22 03:09

Nicola Cossu