MySQL MONTH Function: To Get Month From Date

Get month number from the date in mysql. Through this tutorial, i am going to show you how to get month number from a date using MySQL MONTH() function with the help of examples.

MySQL MONTH() Function

The MONTH() function returns the month part for a given date (a number from 1 to 12). 

Syntax of MySQL MONTH() Function

The MONTH() function syntax is:

MONTH(date)

The date here is the date value that you want the month from which you returned.

Example 1 – Get month number from date in mysql

Take an example to get month number from a date in MySQL; as follows:

SELECT MONTH('2020-06-18') AS 'Result';

Output-1

+--------+
| Result |
+--------+
|     8  |
+--------+

Example 2 – Get month number from current date in mysql

If there is a leading zero in the part of the day, then the leading zero has been left out of the result.

SELECT MONTH('2018-02-01') AS 'Result';

Output-2

+--------+
| Result |
+--------+
|      8 |
+--------+

Example 3 – Get month number from current date in MySQL

 SELECT
 created_at AS create_date,
 MONTH(created_at) AS month
 FROM users
 WHERE id= 112;

Output-3

+---------------------+--------------+
| create_date         | month        |
+---------------------+--------------+
| 2010-08-23 10:33:39 |           8  |
+---------------------+--------------+

Example 4 – Get month number from the current datetime in mysql

To extract part of the month number from the current date and time (which is now returned using the () function); as follows:

  SELECT 
  NOW(),
  MONTH(NOW());

Output-4

+---------------------+--------------------+
| NOW()               | MONTH(NOW())       |
+---------------------+--------------------+
| 2019-07-10 18:30:44 |         7          |
+---------------------+--------------------+

Example 5 – MySQL Month() with CURDATE() Function

Let’s take a another example of using CURDATE() function. Basically CURDATE() function returns only the date without time.

SELECT 
CURDATE(),
MONTH(CURDATE());    

Output-5

+------------+-----------------------+
| CURDATE()  | MONTH(CURDATE())      |
+------------+-----------------------+
| 2019-05-15 |              5        |
+------------+-----------------------+

Conclusion

Through this tutorial, You have learned how to use mysql MONTH() function with various examples.

Leave a Comment