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_id | Commodity_name | Buying_price | Selling_price | Selling_Date |
1 | Audi Q8 | 10000000.00 | 15000000.00 | 2018-01-26 |
2 | Volvo XC40 | 2000000.00 | 3000000.00 | 2018-04-20 |
3 | Audi A6 | 4000000.00 | 5000000.00 | 2018-07-25 |
4 | BMW X5 | 5000500.00 | 7006500.00 | 2018-10-18 |
5 | Jaguar XF | 5000000.00 | 7507000.00 | 2019-01-27 |
6 | Mercedes-Benz C-Class | 4000000.00 | 6000000.00 | 2019-04-01 |
7 | Jaguar F-PACE | 5000000.00 | 7000000.00 | 2019-12-26 |
8 | Porsche Macan | 6500000.00 | 8000000.00 | 2020-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:
MonthName | Commodity_Sold |
April | 3 |
December | 1 |
January | 2 |
July | 1 |
October | 1 |
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.