The QUARTER() function in MySQL is used to retrieve the quarter of a given date. A quarter refers to a three-month period in a calendar year. The function returns an integer from 1 to 4, representing the following quarters:
- 1: January to March
- 2: April to June
- 3: July to September
- 4: October to December
Syntax:
QUARTER(date)
- date: The date or datetime value from which you want to extract the quarter.
Example:
SELECT QUARTER('2024-05-15'); -- Returns 2
SELECT QUARTER('2024-11-01'); -- Returns 4
Explanation:
- In the first example, the date
2024-05-15falls in the second quarter (April to June), so it returns2. - In the second example,
2024-11-01falls in the fourth quarter (October to December), so it returns4.
This function is commonly used in reports and time-based queries to group or categorize data by quarters.
No comments:
Post a Comment