×

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples.

Introduction of MySQL MONTHNAME() function

In MySQL the MONTHNAME() Function is used to find the month name from the given date. It will Return 0 when the MONTH part for the date is 0 or greater than 12; otherwise, it returns the month name between January to December.

Syntax of the MySQL MONTHNAME() Function

The syntax of the MySQL MONTHNAME() function is given as follows:

MONTHNAME(dt)

Parameters or arguments used in MySQL MONTHNAME() Function:

There are two parameters accepted by the MONTHNAME() Function in MySQL, which are given above and described below:

dt: This is the date or datetime from which we want to extract the month name.

Returns:

It will return the month's name from the given date.

Application used for MONTHNAME() function:

The MONTHNAME() Function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL MONTHNAME() function:

Now we will look into some MySQL MONTHNAME() function examples and will explore how we can use the MONTHNAME function in MySQL.

Example-1 :

In this example, we will find the Current Month name Using the MONTHNAME() Function.

SELECT MONTHNAME(NOW()) AS Current_Month;

Output:

Current_Month
December
1 row in set (0.00 sec)

Example-2 :

In this example, we will find the Month name from a given datetime Using the MONTHNAME() Function.

SELECT MONTHNAME('2015-02-26 08:09:22') AS MONTHNAME;

Output:

MONTHNAME
February
1 row in set (0.00 sec)

Example-3 :

In this example, we will find the Month name from the given datetime Using the MONTHNAME() Function when the date is NULL.

SELECT MONTHNAME(NULL) AS MONTHNAME;

Output:

MONTHNAME
NULL
1 row in set (0.00 sec)

Example-4:

The MONTHNAME Function can also be used to find the total Commodity sold for every month. For demonstration, we have created a table named Commodity:

CREATE TABLE Commodity(
Commodity_id INT AUTO_INCREMENT,
Commodity_name VARCHAR(100) NOT NULL,
Buying_price DECIMAL(13, 2) NOT NULL,
Selling_price DECIMAL(13, 2) NOT NULL,
Selling_Date Date NOT NULL,
PRIMARY KEY(Commodity_id)
);

The following Statements insert some data into the Commodity table:

INSERT INTO
Commodity(Commodity_name, Buying_price, Selling_price, Selling_Date)
VALUES
('Audi Q8', 10000000.00, 15000000.00, '2018-01-26' ),
('Volvo XC40', 2000000.00, 3000000.00, '2018-04-20' ),
('Audi A6', 4000000.00, 5000000.00, '2018-07-25' ),
('BMW X5', 5000500.00, 7006500.00, '2018-10-18' ),
('Jaguar XF', 5000000, 7507000.00, '2019-01-27' ),
('Mercedes-Benz C-Class', 4000000.00, 6000000.00, '2019-04-01'),
('Jaguar F-PACE,' 5000000.00, 7000000.00, '2019-12-26' ),
('Porsche Macan', 6500000.00, 8000000.00, '2020-04-16' ) ;

So, Our table will look like this:

mysql> SELECT * FROM Commodity;
Commodity_idCommodity_nameBuying_priceSelling_priceSelling_Date
1Audi Q810000000.0015000000.002018-01-26
2Volvo XC402000000.003000000.002018-04-20
3Audi A64000000.005000000.002018-07-25
4BMW X55000500.007006500.002018-10-18
5Jaguar XF5000000.007507000.002019-01-27
6Mercedes-Benz C-Class4000000.006000000.002019-04-01
7Jaguar F-PACE5000000.007000000.002019-12-26
8Porsche Macan6500000.008000000.002020-04-16

Now, we are going to find the number of Commodities sold per month by using the MONTHNAME() function.

SELECT MONTHNAME(Selling_Date) MonthName,
COUNT(Commodity_id) Commodity_Sold
FROM Commodity
GROUP BY MONTHNAME(Selling_Date)
ORDER BY MONTHNAME(Selling_Date);

Output:

MonthNameCommodity_Sold
April3
December1
January2
July1
October1

Application of MySQL MONTHNAME() function:

This Function is used to find the month's name from the given date.

Summary:

In the above context, we have learned how to use the MONTHNAME() Function in MySQL to find the month name from the given date.


Related Topics

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.