Sunday, December 22, 2024

MySQL MONTHNAME() Function

 The MONTHNAME() function in MySQL is used to return the full name of the month from a given date. This function takes a date or datetime expression as an argument and returns the name of the month in English (or the default language set for the MySQL server).

Syntax:

MONTHNAME(date)
  • date: The date or datetime expression from which you want to extract the month name.

Example Usage:

  1. Basic Example:

    SELECT MONTHNAME('2024-12-23');
    

    Output: December

  2. Using with a column in a table: Suppose you have a table orders with a order_date column of type DATE, and you want to get the month names for all orders:

    SELECT order_id, MONTHNAME(order_date) AS month_name
    FROM orders;
    
  3. With the CURDATE() function: You can also use the MONTHNAME() function with CURDATE() to get the current month's name:

    SELECT MONTHNAME(CURDATE());
    

    Output: It would return the current month's name, e.g., December if run in December.

Notes:

  • The result is returned as a string representing the full name of the month, e.g., "January", "February", etc.
  • The output will be in the language set for the MySQL server. By default, it is in English unless the language is changed.

No comments:

Post a Comment