How to Use QUARTER() Function in MySQL: Syntax and Examples
In MySQL, use the
QUARTER() function to get the quarter (1 to 4) of a given date. For example, QUARTER('2024-05-15') returns 2 because May is in the second quarter of the year.Syntax
The QUARTER() function takes one argument, a date or datetime value, and returns an integer from 1 to 4 representing the quarter of the year.
- QUARTER(date): Returns the quarter number (1 to 4) for the given date.
sql
QUARTER(date)
Example
This example shows how to use QUARTER() to find the quarter of different dates.
sql
SELECT '2024-01-10' AS sample_date, QUARTER('2024-01-10') AS quarter_number; SELECT '2024-05-15' AS sample_date, QUARTER('2024-05-15') AS quarter_number; SELECT '2024-09-30' AS sample_date, QUARTER('2024-09-30') AS quarter_number; SELECT '2024-12-25' AS sample_date, QUARTER('2024-12-25') AS quarter_number;
Output
sample_date | quarter_number
2024-01-10 | 1
sample_date | quarter_number
2024-05-15 | 2
sample_date | quarter_number
2024-09-30 | 3
sample_date | quarter_number
2024-12-25 | 4
Common Pitfalls
Common mistakes when using QUARTER() include:
- Passing a string that is not a valid date format, which returns
NULL. - Using
QUARTER()on a non-date column without conversion. - Expecting zero-based quarters; quarters start at 1, not 0.
sql
/* Wrong: invalid date string returns NULL */ SELECT QUARTER('invalid-date'); /* Right: use valid date string */ SELECT QUARTER('2024-03-31');
Output
NULL
1
Quick Reference
| Function | Description | Returns |
|---|---|---|
| QUARTER(date) | Returns the quarter of the year for the given date | 1, 2, 3, or 4 |
| Valid date formats | Accepts 'YYYY-MM-DD', datetime, or timestamp | Quarter number or NULL if invalid |
| Invalid input | Non-date strings or NULL | Returns NULL |
Key Takeaways
Use QUARTER(date) to get the quarter number (1 to 4) from a date in MySQL.
Ensure the input is a valid date string or date/datetime type to avoid NULL results.
Quarters are 1-based: Q1 = Jan-Mar, Q2 = Apr-Jun, Q3 = Jul-Sep, Q4 = Oct-Dec.
Invalid or malformed dates passed to QUARTER() return NULL.
QUARTER() is useful for grouping or filtering data by quarter in reports.