QUARTER
QUARTER is a SQL function that extracts the quarter from a specified date. It returns an integer value representing the quarter number (1-4) of the provided date. This function is useful when you want to group or categorize dates into their respective quarters of a year in a SQL database.
QUARTER(date)
- date: The date parameter in the QUARTER(date) function for MySQL signifies the date from which the quarter of the year is to be extracted. It must be a valid date in the format “YYYY-MM-DD”, “YYYYMMDD”, “YYMMDD” or a year in string format like “YYYY”. The quarter is returned as an integer in the range from 1 to 4, with 1 representing the first quarter of the year (January, February, March) and 4 representing the last quarter of the year (October, November, December).
Example
Output
Explanation
The QUARTER function in MySQL returns the quarter of the year for a date. The input date ‘2021-07-18’ falls in the third quarter of the year, therefore the output is 3.
QUARTER(date)
- date: It is the date value from which the quarter of the year is to be extracted. This could be a date column in a table or an explicitly input date. It must be a valid expression and data of type date or datetime. The function will return an integer representing the quarter for the provided date ranging from 1 to 4.
Example
Output
Explanation
In SQL Server, the DATEPART()
function is used to extract parts from a date. The example code extracts the quarter from a given date (‘2022-03-15’). The output is ‘1’, which indicates the first quarter of the year.
QUARTER(date)
- date: The parameter refers to a valid date value for which to identify the calendar quarter. It must be a DATE or TIMESTAMP data type. Should the date parameter be omitted or null, then the current date and time will be used as the default value.
Example
Output
Explanation
The EXTRACT(QUARTER FROM DATE '2012-08-08')
function returns the quarter of the year (from 1 to 4) for the specified date. Here it returns 3 since August falls in the third quarter of the year.