In MySQL, the MONTHNAME()
function returns the month name from a date.
For example, if you provide a date of 2021-12-07, the MONTHNAME()
function will return December.
Syntax
The syntax goes like this:
MONTHNAME(date)
Where date
is the date value that you want the month name returned from.
Example
Here’s an example to demonstrate.
SELECT MONTHNAME('2021-12-07') AS 'Result';
Result:
+----------+ | Result | +----------+ | December | +----------+
A Database Example
In this example, I extract the month name from a column when running a query against a database.
USE sakila; SELECT payment_date AS 'Date/Time', MONTHNAME(payment_date) AS 'Month' FROM payment WHERE payment_id = 1;
Result:
+---------------------+-------+ | Date/Time | Month | +---------------------+-------+ | 2005-05-25 11:30:37 | May | +---------------------+-------+
Current Date/Time
Here’s an example of extracting the month name from the current date and time (which is returned using the NOW()
function).
SELECT NOW(), MONTHNAME(NOW());
Result:
+---------------------+------------------+ | NOW() | MONTHNAME(NOW()) | +---------------------+------------------+ | 2018-06-29 15:19:48 | June | +---------------------+------------------+
Another way to do this is to use the CURDATE()
function, which returns only the date (but not the time).
SELECT CURDATE(), MONTHNAME(CURDATE());
Result:
+------------+----------------------+ | CURDATE() | MONTHNAME(CURDATE()) | +------------+----------------------+ | 2018-06-29 | June | +------------+----------------------+
Locale
The language used for the month name is controlled by the lc_time_names
system variable. Here’s an example of changing the value of that variable, and then running MONTHNAME()
again.
SET lc_time_names = 'es_CL'; SELECT MONTHNAME('2021-12-07') AS 'Result';
Result:
+-----------+ | Result | +-----------+ | diciembre | +-----------+
In this example, I changed theĀ lc_time_names
to es_CL
which means Spanish – Chile.