MONTHNAME() Function in MySQL

02 Dec, 2020
MONTHNAME() function in MySQL is used to find month name from the given date. It Returns 0 when MONTH part for the date is 0 or greater than 12 otherwise it returns month name between January to December.
Syntax :
MONTHNAME(date)

Parameter : This method accepts one parameter as mentioned above and described below :
date : The date or datetime from which we want to extract the month name.
Returns : It returns month name from the given date.
Example-1 : Finding the Current Month name Using MONTHNAME() Function.
SELECT MONTHNAME(NOW()) AS Current_Month;

Output :
Example-2 : Finding the Month name from given datetime Using MONTHNAME() Function.
SELECT MONTHNAME('2015-01-26 08:09:22') AS MONTHNAME;

Output :
Example-3 : Finding the Month name from given datetime Using MONTHNAME() Function when the date is NULL.
SELECT MONTHNAME(NULL) AS MONTHNAME;

Output :
Example-4 : The MONTHNAME function can also be used to find total product sold for every month. To demonstrate create a table named.
Product :
CREATE TABLE Product(
  Product_id INT AUTO_INCREMENT,  
  Product_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(Product_id)
);

Now inserting some data to the Product table :
INSERT INTO  
  Product(Product_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 looks like :
mysql> SELECT * FROM Product;
+------------+-----------------------+--------------+---------------+--------------+
| Product_id | Product_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 number of product sold per month by using MONTHNAME() function.
SELECT MONTHNAME(Selling_Date) MonthName,  
COUNT(Product_id) Product_Sold 
FROM Product 
GROUP BY MONTHNAME(Selling_Date)  
ORDER BY MONTHNAME(Selling_Date);

Output :
+-----------+--------------+
| MonthName | Product_Sold |
+-----------+--------------+
| April     |            3 |
| December  |            1 |
| January   |            2 |
| July      |            1 |
| October   |            1 |
+-----------+--------------+

DBMS-SQL
mysql
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.