Hunter McMillen Hunter McMillen - 5 days ago 6
MySQL Question

MySQL MONTHNAME() from numbers

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
?

Answer

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.

Comments