MySQL - MONTHNAME() Function



The MySQL MONTHNAME() function is used to retrieve the name of the month for a given date. It takes a date or datetime as an argument and returns the full name of the month associated with that date.

A NULL value is returned if the date argument passed to the function is NULL.

Syntax

Following is the syntax of MySQL MONTHNAME() function −

MONTHNAME(date);

Parameters

This method accepts the date value from which you need to retrieve the month name as a parameter.

Return value

This function returns the name of the month for a given date.

Example

In the following example, we are using the MysSQL MONTHNAME() function to fetch the name of the month from the given date value −

SELECT MONTHNAME('2023-11-22') As Result;

Output

This will produce the following result −

Result
November

Example

If the month part in the given date is 0 this function returns NULL

SELECT MONTHNAME('2017-00-00') As Result;

Following is the output −

Result
NULL

If we pass an empty string or a non-string value as an argument this function returns NULL

SELECT MONTHNAME('') As Result;

Following is the output −

Result
NULL

Here, we are passing a non-string value as an argument to this function −

SELECT MONTHNAME(1990-11-11) As Result;

Following is the output −

Result
NULL

Example

In the following query, we are retrieving the month name from the current date −

SELECT MONTHNAME(CURDATE()) As Result;

Output

Following is the output −

Result
November

Example

In the following example, let us create a table named ORDERS using CREATE TABLE statement −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:00 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

Here, we use MONTHNAME() function to fetch the monthname for all the datetime values in the DATE column −

SELECT OID, DATE, MONTHNAME(DATE) as Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Result
102 2009-10-08 00:00:00 October
100 2009-10-08 00:00:00 October
101 2009-11-20 00:00:00 November
103 2008-05-20 00:00:00 May
Advertisements