How the MONTHNAME() function works in Mariadb?
The MONTHNAME()
function is a date and time function that returns the name of the month for a given date.
The MONTHNAME()
function is a date and time function that returns the name of the month for a given date. The name of the month is returned as a string in the current language setting of the server. The function can also accept an optional second argument that specifies the locale for the month name.
Syntax
The syntax of the MONTHNAME()
function is as follows:
MONTHNAME(date)
The date
argument is a valid date or datetime expression that represents the date for which the month name is returned. The date
argument can also be a string in a format that can be converted to a date or datetime value.
Examples
Example 1: Basic usage
The following example shows how to use the MONTHNAME()
function to return the name of the month for a given date:
SELECT MONTHNAME('2024-02-13');
The output is:
February
The function returns the name of the month in English, as it is the default language setting of the server.
Example 2: Using a datetime expression
The following example shows how to use the MONTHNAME()
function with a datetime expression as the date
argument:
SELECT MONTHNAME(NOW());
The output is:
February
The function returns the name of the current month, as it evaluates the NOW()
function to get the current datetime value.
Example 3: Handling invalid arguments
The following example shows how the MONTHNAME()
function handles invalid arguments:
SELECT MONTHNAME('2024-02-31');
The output is:
NULL
The function returns NULL
, as it cannot parse the string as a valid date.
Related Functions
Some of the functions that are related to the MONTHNAME()
function are:
MONTH()
function: Returns the month number for a given date, ranging from 1 to 12.DAYNAME()
function: Returns the name of the weekday for a given date.DATE_FORMAT()
function: Returns a formatted string based on a date or datetime value and a format specifier.STR_TO_DATE()
function: Returns a date or datetime value based on a string and a format specifier.
For example, the following query uses the MONTH()
and DAYNAME()
functions to return the month number and the weekday name for a given date:
SELECT MONTH('2024-02-13') AS month_number, DAYNAME('2024-02-13') AS weekday_name;
The output is:
+--------------+--------------+
| month_number | weekday_name |
+--------------+--------------+
| 2 | Tuesday |
+--------------+--------------+
Conclusion
The MONTHNAME()
function is a useful function to get the name of the month for a given date. The function can also accept a locale argument to return the month name in a different language. The function can handle various types of arguments, such as date, datetime, or string values. The function is related to other date and time functions, such as MONTH()
, DAYNAME()
, DATE_FORMAT()
, and STR_TO_DATE()
functions.