MySQL - QUARTER() Function



The MySQL QUARTER() function is used to retrieve and return the quarter year of the given date or, date time expression.

An year consists of 12 months; which can be divided into two halves or four quarters. In the halves, the first six months belong to the first half, whereas the last six months belong to the second half. Similarly, the quarters in a year are divided by 3 months each. First three months belong to first quarter, next three belong the second quarter, and so on. Each of these quarters are numbered from 1 to 4; therefore, the function only returns values ranging from 1 to 4.

If the argument passed to this function is an empty string or a NULL value, the return value will also be obtained as a NULL value.

Syntax

Following is the syntax of MySQL QUARTER() function −

QUARTER(date);

Parameters

This method accepts the date value from which you need to retrieve the quarter as a parameter.

Return value

This function returns the quarter year of the given date or, date time expression.

Example

Following example demonstrates the usage of the QUARTER() function −

SELECT QUARTER('2019-05-25') As Result;

Output

Following output is obtained −

Result
2015-09-05

Example

If the month value in the given date is 0 this function returns 0 −

SELECT QUARTER('0000-00-07') As Result;

Output

Following output is obtained −

Result
0

Example

If we pass an empty string or a non-string value as an argument to this function returns NULL.

SELECT QUARTER('') As Result;

Following output is obtained −

Result
NULL

Here, we are passing a non-string value as an argument to this function −

SELECT QUARTER(1990-11-11) As Result;

Following output is obtained −

Result
NULL

Example

We can also pass the date-time expression as an argument to this function −

SELECT QUARTER('2015-09-05 09:40:45.2300') As Result;

Output

Following output is obtained −

Result
3

Example

In the following example, we are retrieving the quarter of the year from the current timestamp −

SELECT QUARTER(CURRENT_TIMESTAMP()) As Result;

Output

Following output is obtained −

Result
4

Example

In this example, we have created a table named ORDERS using the following CREATE TABLE query −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:00 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

Following query retrieves the date values from the DATE column of ORDERS table −

SELECT OID, DATE, QUARTER(DATE) As Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Result
102 2009-10-08 00:00:00 4
100 2009-10-08 00:00:00 4
101 2009-11-20 00:00:00 4
103 2008-05-20 00:00:00 2
Advertisements