In MariaDB, MONTHNAME()
is a built-in date and time function that returns the name of the month name for a given date.
It accepts one argument, which is the date you want to extract the month name from.
Syntax
The syntax goes like this:
MONTHNAME(date)
Where date
is the date to get the month name from.
Example
Here’s an example:
SELECT MONTHNAME('2041-11-14');
Result:
+-------------------------+ | MONTHNAME('2041-11-14') | +-------------------------+ | November | +-------------------------+
Language
The language used for the month name is controlled by the value of the lc_time_names
system variable.
Here’s an example that shows how this affects the result:
SET lc_time_names = 'es_AR';
SELECT MONTHNAME('2041-11-14');
Result:
+-------------------------+ | MONTHNAME('2041-11-14') | +-------------------------+ | noviembre | +-------------------------+
And switching back to en_US
, which is the default:
SET lc_time_names = 'en_US';
SELECT MONTHNAME('2041-11-14');
Result:
+-------------------------+ | MONTHNAME('2041-11-14') | +-------------------------+ | November | +-------------------------+
Here’s a list of locales supported by MariaDB, and here’s how to return your own list of available locales.
And here’s a post explaining how to check the value of your lc_time_names
system variable.
Datetime Values
It also works with datetime values:
SELECT MONTHNAME('2041-01-15 10:30:45');
Result:
+----------------------------------+ | MONTHNAME('2041-01-15 10:30:45') | +----------------------------------+ | January | +----------------------------------+
Zero Months
Having a zero month in the date returns null
:
SELECT MONTHNAME('2030-00-00');
Result:
+-------------------------+ | MONTHNAME('2030-00-00') | +-------------------------+ | NULL | +-------------------------+
Numeric Dates
It’s also possible to pass dates as a number, as long as it makes sense as a date.
Example
SELECT MONTHNAME(20201208);
Result:
+---------------------+ | MONTHNAME(20201208) | +---------------------+ | December | +---------------------+
Two-digit years are fine:
SELECT MONTHNAME(201208);
Result:
+-------------------+ | MONTHNAME(201208) | +-------------------+ | December | +-------------------+
But it must make sense as a date. Here’s what happens if I increase the day part to an invalid day:
SELECT MONTHNAME(201299);
Result:
+-------------------+ | MONTHNAME(201299) | +-------------------+ | NULL | +-------------------+
Other Delimiters
You can use other delimiters for the date. MariaDB is quite forgiving when it comes to delimiters on dates. Here are some valid examples:
SELECT
MONTHNAME('2027/08/19'),
MONTHNAME('2027,08,19'),
MONTHNAME('2027:08:19'),
MONTHNAME('2027;08!19');
Result (using vertical output):
MONTHNAME('2027/08/19'): August MONTHNAME('2027,08,19'): August MONTHNAME('2027:08:19'): August MONTHNAME('2027;08!19'): August
Current Date
We can pass NOW()
as the date argument to use the current date:
SELECT
NOW(),
MONTHNAME(NOW());
Result:
+---------------------+------------------+ | NOW() | MONTHNAME(NOW()) | +---------------------+------------------+ | 2021-05-14 10:11:16 | May | +---------------------+------------------+
Invalid Arguments
When passed an invalid argument, MONTHNAME()
returns null
:
SELECT MONTHNAME('Wrong!');
Result:
+---------------------+ | MONTHNAME('Wrong!') | +---------------------+ | NULL | +---------------------+
Missing Argument
Calling MONTHNAME()
with the wrong number of arguments, or without passing any arguments, results in an error:
SELECT MONTHNAME();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'MONTHNAME'
And another example:
SELECT MONTHNAME('2030-01-25', '2045-05-08');
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'MONTHNAME'