MariaDB QUARTER() Function

In MariaDB, QUARTER() is a built-in function that returns the quarter from a given date expression.

MariaDB QUARTER() Syntax

This is the syntax of the MariaDB QUARTER() function:

QUARTER(date)

Parameters

date

Required. A date or datetime expression.

Return value

The MariaDB QUARTER() function returns a quarter value for a specified date. The return value of the QUARTER() function is between 1 to 4, and their meanings are as follows:

  • 1 - The First quarter, from January to March.
  • 2 - The Second quarter, from April to June.
  • 3 - The Third quarter, from July to September.
  • 4 - The Fourth quarter, from October to December.

If the argument is NULL, the QUARTER() function will return NULL.

If the month in the date is 0, the QUARTER() function will return 0.

MariaDB QUARTER() Examples

Basic example

The following statement shows the basic usage of the MariaDB QUARTER() function:

SELECT
  QUARTER('2012-01-01'),
  QUARTER('2012-04-01 10:11:12');

Output:

+-----------------------+--------------------------------+
| QUARTER('2012-01-01') | QUARTER('2012-04-01 10:11:12') |
+-----------------------+--------------------------------+
|                     1 |                              2 |
+-----------------------+--------------------------------+

Zero month

If the month in the date is 0, the QUARTER() function will return 0.

SELECT QUARTER('2012-00-00');

Output:

+-----------------------+
| QUARTER('2012-00-00') |
+-----------------------+
|                     0 |
+-----------------------+

Digital date

The MariaDB QUARTER function allow you to pass a date as a number, but you must provide a valid date.

SELECT
  QUARTER(20230101),
  QUARTER(230101);

Output:

+-------------------+-----------------+
| QUARTER(20230101) | QUARTER(230101) |
+-------------------+-----------------+
|                 1 |               1 |
+-------------------+-----------------+

Other delimiters

The MariaDB QUARTER() function allow you to construct dates with various separators:

SELECT
    QUARTER('2023/01/10'),
    QUARTER('2023,01!10'),
    QUARTER('2023#01%10');

Output:

+-----------------------+-----------------------+-----------------------+
| QUARTER('2023/01/10') | QUARTER('2023,01!10') | QUARTER('2023#01%10') |
+-----------------------+-----------------------+-----------------------+
|                     1 |                     1 |                     1 |
+-----------------------+-----------------------+-----------------------+

Current date

We can pass NOW() as the parameter to get quarter:

SELECT
    NOW(),
    QUARTER(NOW());

Output:

+---------------------+----------------+
| NOW()               | QUARTER(NOW()) |
+---------------------+----------------+
| 2023-01-11 17:00:40 |              1 |
+---------------------+----------------+

Conclusion

In MariaDB, QUARTER() is a built-in function that returns the quarter from a given date expression.