MS SQL Server

How to Use the MONTHNAME() Function in MySQL?

If you’re working with a MySQL database and need to retrieve the month name from a given date value, the MONTHNAME() function is exactly what you need. More specifically, the MONTHNAME() function helps us in extracting the name of the month from a given date value. By utilizing the MONTHNAME() function in MySQL, you can easily retrieve, manage and manipulate large amounts of data based on a specific month.

This guide will elaborate on how to use the MONTHNAME() function in MySQL.

How to Utilize the MONTHNAME() Function in MySQL?

The “MONTHNAME()” is an inbuilt function in MySQL that can be utilized with the “SELECT” statement to get the month name from the given date value. The syntax to use the MONTHNAME() function in MySQL is given below:

MONTHNAME(date);

In the above syntax, replace the “date” with a date value from which you want to extract the month name.

Let’s head toward the few examples to comprehend the use cases of the MONTHNAME() function in MySQL.

Example 1: Retrieving Month Name From Current Date and Time
The “NOW()” function in MySQL returns the current date and time, which can be utilized as an argument in the “MONTHNAME()” function to get the name of the present/current month. The query to use the “MONTHNAME()” function with “NOW()” function is as follows:

SELECT MONTHNAME(NOW());

Output

The output showed the current month value as “April”.

Example 2: Getting Month Name From Current Date
The “CURDATE()” function is utilized to retrieve the current date value, which can also be used with the “MONTHNAME()” function to get the current month’s name. The query is given below:

SELECT MONTHNAME(CURDATE());

Output

The output displayed the current month’s name.

Example 3: Getting Month Name From Static Date
The “MONTHNAME()” function can be utilized to get the month names from the static date value. Here is an example of retrieving the month name from a static date:

SELECT MONTHNAME('2023-03-25');

In the above example, “2023-03-25” is the static date value.

Output

The output depicts the month name extracted from the provided static date value (i.e., 2023-03-25).

Example 4: Getting Month Name From Table Date Values
The month name can be fetched from the table’s data by utilizing the “MONTHNAME()” function. An example of retrieving the month name value from the “joineddate” column of the “lh_data” table is provided below:

SELECT *, MONTHNAME(joineddate) AS Month_Name FROM lh_data;

Output

The output displayed the month names from the “joineddate” column.

Example 4: Getting Month Name Under the Particular Condition
To get the month name from the table’s data based on a specific condition, use the “WHERE” clause along with the “MONTHNAME()” function. The example of retrieving the month name value from the “joineddate” column of the “lh_data” table under the particular condition is as follows:

SELECT id, joineddate, MONTHNAME(joineddate) AS Month_Name
FROM lh_data
WHERE id > 7;

In the above example, a condition is specified to extract the month name from only those rows that have the “id” greater than 7.

Output

That was all about the usage of the MONTHNAME() function for the different purposes in MySQL.

Conclusion

The MONTHNAME() function in MySQL is a useful tool for retrieving the name of the month from a given date value. It can be used in various scenarios, such as getting the current month name, retrieving month names from static dates, or extracting the month names from a table column. This guide has presented detailed information on the use cases of the MONTHNAME() Function in MySQL.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.