MySQL Month Function

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

MONTH(date or expression);

MySQL Month function Example

This MySQL Month function 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 Month function. Here, we are returning the Month numbers from the different date expression, and the Date & time expression.

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

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

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

MySQL Month Function Example 2

The following Date Function query shows you what happens when we try to return the month number from a date in string format. Here, we are returning month number from the current date and time returned by the Now() function, CURDATE() function, and UTC_TIMESTAMP function.

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

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

SELECT UTC_TIMESTAMP(), MONTH(UTC_TIMESTAMP());
MySQL Month Function 2

Here, we are trying to return the month number from YYYYMMDD, 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');
MySQL Month Function 3

Month Example 3

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

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