Home > Database > Mysql Tutorial > How to Retrieve Month Names from Numerical Values in MySQL?

How to Retrieve Month Names from Numerical Values in MySQL?

Patricia Arquette
Release: 2024-11-27 17:51:12
Original
217 people have browsed it

How to Retrieve Month Names from Numerical Values in MySQL?

Retrieving Month Names from Numerical Month Values with MySQL

MySQL provides limited native functionality for converting numerical month values (1-12) to their corresponding month names. However, there is a workaround using the STR_TO_DATE() and MONTHNAME() functions:

  1. Convert the Numerical Month: Convert the numerical month value to a date using STR_TO_DATE(). For instance, the syntax for converting the month number 6 to a date would be:
STR_TO_DATE(6, '%m')
Copy after login

This would result in the date '2000-06-01'.

  1. Extract Month Name: Next, use the MONTHNAME() function on the converted date to extract the corresponding month name. For example, to extract the month name for the date '2000-06-01':
MONTHNAME(STR_TO_DATE(6, '%m'))
Copy after login

The output of this query would be 'June'.

Example:

SELECT MONTHNAME(STR_TO_DATE(6, '%m'));

+---------------------------------+
| MONTHNAME(STR_TO_DATE(6, '%m')) |
+---------------------------------+
| June                            |
+---------------------------------+
Copy after login

Caution: While this method is effective, it may be slow when applied to a large number of rows due to the conversion process involved.

The above is the detailed content of How to Retrieve Month Names from Numerical Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template