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
?
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.
MySQL MONTHNAME() Function The MONTHNAME() function returns the name of the month for a given date.
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.
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.
A somewhat ugly way would be SELECT MONTHNAME(CONCAT('2011-',8,'-01'));
Before reading Michael's great answer I had thought something like this
select elt(3,'January','February','March',....)
but his one is much better. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With