MySQL QUARTER Function

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

QUARTER(date or expression);

TIP: This method returns 0 if the given argument is 0000-00-00. Or Say it returns 0 if the Zero DATEPART is given.

MySQL Quarter function Example

The below-shown queries help you understand the use of this method. Here, we are returning the Quarter numbers from the different date expressions and the Datetime expression.

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

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

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

Here, we are returning the 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);
Get QUARTER from Current Date and Now Example 2

Example 2

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

SELECT QUARTER(20190512);

SELECT QUARTER(750812);

SELECT QUARTER(20191112012233);
Example 3

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

SELECT QUARTER(20190542);

SELECT QUARTER(2019-00-00);

SELECT QUARTER(CURDATE() + 45);
Example 4

In this instance, we show you how to use this method on a table. Here, we are returning the month number from the HireDate column. For this 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 customer;
MySQL QUARTER Function 5