The QUARTER() function is used to return the quarter of the year for a given date. The result will be an integer value from 1 to 4, depending on the month of the provided date.
Definition and Usage
- Purpose: Returns the quarter of the year for a given date.
- 1: January-March
- 2: April-June
- 3: July-September
- 4: October-December
Syntax
QUARTER(date)
Parameters
- date: The date or datetime from which the quarter of the year is to be extracted.
Technical Details
- Works in: MySQL 4.0 and later.
- Return Type: Integer (1 to 4).
Examples
Example 1: Extract the Quarter for a Specific Date
SELECT QUARTER("2017-06-15");
Result: 2
Explanation: June 15, 2017, falls in the second quarter of the year (April to June).
Example 2: Extract the Quarter for a Date (Datetime Format)
SELECT QUARTER("2017-01-01 09:34:21");
Result: 1
Explanation: January 1, 2017, falls in the first quarter of the year (January to March).
Example 3: Extract the Quarter for the Current System Date
SELECT QUARTER(CURDATE());
Result: Returns the quarter of the current date.
Use Cases
- Financial Reporting: To break down data into quarters (e.g., Q1, Q2, Q3, Q4).
- Quarterly Analysis: For analysis based on the quarterly performance or statistics.
- Period Segmentation: Useful in applications where events, sales, or reports are divided by quarters.
Key Notes
- The function returns an integer (1-4) representing the quarter of the year.
- If the
dateprovided is invalid or in the wrong format, the query will return an error.