MySQL MONTHNAME() Function

MySQL MONTHNAME() Function

The MONTHNAME() function is used to extract the name of the month from a given date or datetime value.


Definition and Usage

  • Purpose: Retrieve the full name of the month for a specified date or datetime.
  • Output Format: Returns the month name as a string (e.g., January, February).

Syntax

MONTHNAME(date)

Parameter Values

Parameter Description
date Required. The date or datetime value to extract the month name from.

Technical Details

  • Introduced in: MySQL 4.0 and later.
  • Return Type: A string representing the full month name.

Examples

Example 1: Extracting Month Name from a Date

SELECT MONTHNAME("2017-06-15");

Result: June

Example 2: Extracting Month Name from a Datetime

SELECT MONTHNAME("2017-06-15 09:34:21");

Result: June

Example 3: Extracting Month Name for the Current Date

SELECT MONTHNAME(CURDATE());

Result: Current month's name (e.g., December if run in December).

Example 4: Using MONTHNAME() in a Query

SELECT customer_id, order_date FROM orders WHERE MONTHNAME(order_date) = 'December';

Use Case: Retrieve orders placed in December.

Example 5: Invalid Input

SELECT MONTHNAME("invalid-date");

Result: NULL
(Invalid date values return NULL.)


Key Notes

  1. The MONTHNAME() function works with both DATE and DATETIME values.
  2. If the input value is NULL or invalid, the function returns NULL.
  3. Often used in conjunction with MONTH() for both name and number-based filtering.

Practical Uses

  • Reporting: Display human-readable month names in reports.
  • Filters: Retrieve records by month name.
  • Data Analysis: Analyze trends based on months using descriptive labels instead of numbers.

Was this article helpful?