MySQL QUARTER() Function

MySQL QUARTER() function; In this tutorial, i am going to show you MySQL quarter() function with the help of examples.

MySQL QUARTER() Function

QUARTER() function in MySQL is used to return the quarter of the year for a given date value. It returns a number from 1 to 4. date : The date or DateTime from which we want to extract the quarter.

Syntax of MySQL QUARTER() Function

The syntax of the quarter() function is:

QUARTER(date)

Here date is the date which you want to extract the quarter from.

Example 1 – MySQL QUARTER() Function

Let’s take a simple and basic example of this quarter() function; as follows:

SELECT QUARTER('2019-07-21');

Output-1

 +-----------------------+
 | QUARTER('2019-07-21') |
 +-----------------------+
 |                     3 |
 +-----------------------+

Example 2 – MySQL QUARTER() Function

Let’s take another example with the date have an out of range; as follows:

SELECT QUARTER('2019-07-32');

Result:

+-----------------------+
| QUARTER('2019-07-32') |
+-----------------------+
|                  NULL |
+-----------------------+

Example 3 – MySQL QUARTER() Function

Let’s take an example using mysql quarter function; as follows:

SELECT QUARTER(20190721);

Output-3

+-------------------+
| QUARTER(20190721) |
+-------------------+
|                 3 |
+-------------------+

Example 4 – MySQL QUARTER() with CURDATE Function

Let’s see the example using curdate() function that extracts the quarter from the current date; as follows:

    SELECT 
    CURDATE() AS 'Current Date',
    QUARTER(CURDATE()) AS 'Quarter';

Output-4

+--------------+---------+
| Current Date | Quarter |
+--------------+---------+
| 2019-07-21   |       3 |
+--------------+---------+

Example 5 – MySQL QUARTER() Function

Let’s take an example to fetch the data from the table using this MySQL query; as follows:

USE sakila;
SELECT
    created_at AS 'Create Date',
    QUARTER(created_at) AS 'Quarter'
FROM test
WHERE id = 1;

Output-5

+---------------------+---------+
| Created Date        | Quarter |
+---------------------+---------+
| 2019-07-21 11:40:47 |       3 |
+---------------------+---------+

Conclusion

MySQL QUARTER() Function tutorial, you have learned how to use MySQL QUARTER() function with various examples.

Leave a Comment