MySQL QUARTER Function

MySQL Quarter function is one of the MySQL Date Functions, which is used to return the quarter of the year number from a given date. This Quarter function returns integer range from 1 to 4. The basic syntax of the Quarter Function in MySQL is as shown below:

QUARTER(date or expression);

TIP: This Quarter function returns 0 if the given date argument is 0000-00-00. Or Say, it returns 0 if the Zero date part is given.

MySQL Quarter function Example

The below shown queries help you understand the use of this Quarter function. Here, we are returning the Quarter numbers from the different date expression, and the Date & time expression.

SELECT QUARTER('2016-12-22');

SELECT QUARTER('2016-07-15');

SELECT QUARTER('2016-03-15 11:22:33');
MySQL QUARTER Function 1

Here, we are returning quarter of the year from the current date and time returned by the Now() and CURDATE() function.

SELECT CURDATE(), QUARTER(CURDATE());

SELECT NOW(), QUARTER(NOW());

SELECT QUARTER(CURDATE() + 20), QUARTER(NOW() + 2);
MySQL QUARTER Function 2

MySQL Quarter Function Example 2

The following query shows you what happens when we try to return the year quarter value from a date in string format. In this Date Function example, we are trying to return the quarter of the year from YYYYMMDD, YYMMDD, YYYYMMDDHHMMSS formats.

SELECT QUARTER(20190512);

SELECT QUARTER(750812);

SELECT QUARTER(20191112012233);
MySQL QUARTER Function 3

Here, we are trying to return the Quarter of the Year from an invalid date, and zero month part. That’s why MySQL returns NULL.

SELECT QUARTER(20190542);

SELECT QUARTER(2019-00-00);

SELECT QUARTER(CURDATE() + 45);
MySQL QUARTER Function 4

Quarter Example 3

In this instance, we show you how to use this Quarter function on a table. Here, we are returning the month number from the Hire Date column. For this MySQL Quarter function demo, we are using Workbench to write a query against the customer table.

SELECT EmpID,
FirstName,
LastName,
Occupation,
YearlyIncome,
Sales,
HireDate,
QUARTER(HireDate)
FROM `MySQL Tutorial`.customer;
MySQL QUARTER Function 5
\