Sunday, December 22, 2024

MySQL QUARTER() Function

 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-15 falls in the second quarter (April to June), so it returns 2.
  • In the second example, 2024-11-01 falls in the fourth quarter (October to December), so it returns 4.

This function is commonly used in reports and time-based queries to group or categorize data by quarters.

No comments:

Post a Comment