w3resource

MySQL MONTHNAME() function

MONTHNAME() function

MySQL MONTHNAME() returns the full name of the month for a given date. The return value is within the range of 1 to 12 ( January to December). It Returns NULL when month part for the date is 0 or more than 12. It provides a way to obtain the month's name for various date-related calculations and data presentation.

This function is useful in -

  • MONTHNAME() is valuable for presenting date-based data in a human-readable format, enhancing data visualization.
  • The function is valuable for aggregating and summarizing data by month name, providing insights into monthly trends.
  • MONTHNAME() aids in analyzing and categorizing data based on the names of the months.
  • The function is useful in international applications, as it returns month names based on the language settings.
  • MONTHNAME() can be used to generate custom reports that include the full names of the months.
  • MONTHNAME() helps in time series analysis, where you can create timelines with month names as labels.
  • The function is used to create meaningful labels for events or activities that occurred in specific months.

Syntax:

MONTHNAME(date1)

Where date1 is a date.

Syntax Diagram:

MySQL MONTHNAME() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial Presentation:

Pictorial Presentation of MySQL MONTHNAME() function

Example: MySQL MONTHNAME() function

The following statement will return the full name of the month for the given date 2009-05-18.

Code:

SELECT MONTHNAME('2009-05-18');

Output:

mysql> SELECT MONTHNAME('2009-05-18');
+-------------------------+
| MONTHNAME('2009-05-18') |
+-------------------------+
| May                     | 
+-------------------------+
1 row in set (0.02 sec)

Example: MONTHNAME() function using table

The following statement will return invoice_no and the full name of the month for the invoice_dt from the table purchase.

Sample table: purchase


Code:

SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase;

Output:

mysql> SELECT invoice_no,MONTHNAME(invoice_dt)
    -> FROM purchase;
+------------+-----------------------+
| invoice_no | MONTHNAME(invoice_dt) |
+------------+-----------------------+
| INV0001    | July                  | 
| INV0002    | August                | 
| INV0003    | September             | 
| INV0004    | August                | 
| INV0005    | July                  | 
| INV0006    | September             | 
+------------+-----------------------+
6 rows in set (0.05 sec)

Example: MONTHNAME() function with where clause

The following statement will return invoice_no and the full name of the month for the invoice_dt as MONTHNAME(invoice_dt) from the table purchase, making sure that value of the MONTHNAME(invoice_dt) is more than 7 (i.e. July).

Sample table: purchase


Code:

SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase            
WHERE MONTH(invoice_dt)>7;

Output:

mysql> SELECT invoice_no,MONTHNAME(invoice_dt)
    -> FROM purchase            
    -> WHERE MONTH(invoice_dt)>7;
+------------+-----------------------+
| invoice_no | MONTHNAME(invoice_dt) |
+------------+-----------------------+
| INV0002    | August                | 
| INV0003    | September             | 
| INV0004    | August                | 
| INV0006    | September             | 
+------------+-----------------------+
4 rows in set (0.00 sec)

Video Presentation:

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: MONTH()
Next: NOW()



Follow us on Facebook and Twitter for latest update.