2 Ways to Find Out Which Quarter a Date Belongs to in MariaDB

If you ever need to find out which quarter a date belongs to in MariaDB, here are two functions that will allow you to do just that.

The QUARTER() Function

Yes, there’s actually a whole function dedicated to returning the quarter from a date value. Simply pass the date to the function to extract the quarter.

Example:

SELECT QUARTER('2024-01-01');

Result:

+-----------------------+
| QUARTER('2024-01-01') |
+-----------------------+
|                     1 |
+-----------------------+

In this case, the date belongs to the first quarter.

Here are some more dates to demonstrate each of the four quarters:

SELECT 
    QUARTER('2024-02-23'),
    QUARTER('2024-04-01'),
    QUARTER('2024-08-30'),
    QUARTER('2024-12-31');

Result (using vertical output):

QUARTER('2024-02-23'): 1
QUARTER('2024-04-01'): 2
QUARTER('2024-08-30'): 3
QUARTER('2024-12-31'): 4

The EXTRACT() Function

Another function that enables you to return the quarter from a date is the EXTRACT() function. Using this function, you specify the date and time unit that you want to extract, along with the date.

Example:

SELECT EXTRACT(QUARTER FROM '2024-12-12');

Result:

+------------------------------------+
| EXTRACT(QUARTER FROM '2024-12-12') |
+------------------------------------+
|                                  4 |
+------------------------------------+

In this case, the date belongs to the fourth quarter.

Here are some more dates to demonstrate each of the four quarters:

SELECT 
    EXTRACT(QUARTER FROM '2024-01-01'),
    EXTRACT(QUARTER FROM '2024-05-01'),
    EXTRACT(QUARTER FROM '2024-09-01'),
    EXTRACT(QUARTER FROM '2024-11-01');

Result (using vertical output):

EXTRACT(QUARTER FROM '2024-01-01'): 1
EXTRACT(QUARTER FROM '2024-05-01'): 2
EXTRACT(QUARTER FROM '2024-09-01'): 3
EXTRACT(QUARTER FROM '2024-11-01'): 4