MySQL Month Function

MySQL Month Date Functions, which returns the Month number from a given date. This Month function returns an integer range from 1 to 12, where 1 = January and 12 = December, and the basic syntax is as shown below:

MONTH(date or expression);

MySQL Month function Example

This method returns 0 if the user-specified date argument is 0000-00-00 or 2019-00-00. Or, Say, it returns 0 if the Zero Month part is given. The below shown queries help you understand the use of this MySQL Month function. Here, we are returning the Month numbers from the different date expressions and the Date & time expressions.

SELECT MONTH('2015-12-22');

SELECT MONTH('2015-02-20 01:22:14');

SELECT MONTH('2016-08-20');
MySQL MONTH Function Example 1

Month Example 2

The following MySQL function query shows you what happens when we try to get the month number from a date in string format.

Here, we are printing the month number from the current datetime returned by the Now(), CURDATE(), and UTC_TIMESTAMP of methods.

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

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

SELECT UTC_TIMESTAMP(), MONTH(UTC_TIMESTAMP());
Example 2

Here, we are trying to return the month number from YYYYMMDD and YYMMDD formats. Within the last MySQL statement, we used the zero month part. That’s why it returns NULL.

SELECT MONTH('20160922');

SELECT MONTH('160212');

SELECT MONTH('2019-00-00'), MONTH('0');
Month Example 3

In this example, let us see how to use this MySQL Month function on a table. Here, we are printing the month number from the HireDate column. For this demo, we are using Workbench to write a query against the customer table.

SELECT EmpID,
       FirstName,
       LastName,
       Occupation,
       YearlyIncome,
       Sales,
       HireDate,
       MONTH(HireDate)
 FROM customer;
MySQL Month Function 4