QUARTER() Function Examples – MySQL

Here, we will demonstrate how to works the quarter() function of mysql and how to use this function with various examples.

MySQL QUARTER() Function

In MySQL, the QUARTER() function is used to return the quarter of the year of a given date.

Note: You can pass one parameter in these functions, this will extract the quarter from the parameter.

Syntax

The syntax of the quarter() function is:

QUARTER(date)

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

Example-1

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

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

Output-1

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

Example-2

Let’s take another example with the date have an out of range, in that case, you’ll get a null value:

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

Result:

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

Example-3

Now we take the next example for the function:

SELECT QUARTER(20190721);

Output-3

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

Example-4

Let’s see the example using curdate() function that extracts the quarter from the current date.

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

Output-4

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

Example-5

Next, we take an example of the database. We use the database table to fetch the data from the table using this MySQL query:

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

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

If you have any questions or thoughts to share, use the comment form below to reach us.

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *