In MySQL, the QUARTER()
function returns the quarter of the year of a given date.
This function accepts one argument – the date to extract the quarter from.
Syntax
The syntax goes like this:
QUARTER(date)
Where date
is the date you want to extract the quarter from.
Example 1 – Basic Usage
Here’s an example to demonstrate.
SELECT QUARTER('1999-12-31');
Result:
+-----------------------+ | QUARTER('1999-12-31') | +-----------------------+ | 4 | +-----------------------+
If you have an out-of-range date, you’ll get a null value:
SELECT QUARTER('1999-12-32');
Result:
+-----------------------+ | QUARTER('1999-12-32') | +-----------------------+ | NULL | +-----------------------+
You can also provide the date like this:
SELECT QUARTER(19991231);
Result:
+-------------------+ | QUARTER(19991231) | +-------------------+ | 4 | +-------------------+
Example 2 – Using the Current Date
Here’s an example that extracts the quarter from the current date.
SELECT CURDATE() AS 'Current Date', QUARTER(CURDATE()) AS 'Quarter';
Result:
+--------------+---------+ | Current Date | Quarter | +--------------+---------+ | 2018-07-01 | 3 | +--------------+---------+
Example 3 – A Database Example
Here’s an example that uses a database query.
USE sakila; SELECT payment_date AS 'Payment Date', QUARTER(payment_date) AS 'Quarter' FROM payment WHERE payment_id = 1;
Result:
+---------------------+---------+ | Payment Date | Quarter | +---------------------+---------+ | 2005-05-25 11:30:37 | 2 | +---------------------+---------+