How the QUARTER() function works in Mariadb?
The QUARTER()
function is a date and time function in Mariadb that returns the quarter of the year for a given date.
The QUARTER()
function is a date and time function in Mariadb that returns the quarter of the year for a given date. The quarter of the year is a number from 1 to 4, where 1 represents the first quarter (January to March), 2 represents the second quarter (April to June), 3 represents the third quarter (July to September), and 4 represents the fourth quarter (October to December). The function can be used to perform various calculations and analysis involving quarters, such as grouping, filtering, or aggregating data by quarters.
Syntax
The syntax of the QUARTER()
function is as follows:
QUARTER(date)
The function takes one argument:
date
: A date or datetime expression that represents the date for which the quarter is to be returned. The date or datetime expression can be any valid value, such as a literal, a column, a function, or a variable. The date or datetime expression cannot beNULL
.
The function returns an integer that represents the quarter of the year for the given date, as follows:
- 1: If the date is in the first quarter (January to March).
- 2: If the date is in the second quarter (April to June).
- 3: If the date is in the third quarter (July to September).
- 4: If the date is in the fourth quarter (October to December).
Examples
Example 1: Finding the quarter of a date
The following example finds the quarter of the date ‘2021-05-15’ using the QUARTER()
function.
SELECT QUARTER('2021-05-15') AS quarter;
The output is:
+---------+
| quarter |
+---------+
| 2 |
+---------+
The output shows that the date ‘2021-05-15’ is in the second quarter of the year, as it is in the month of May.
Example 2: Finding the quarter of a datetime
The following example finds the quarter of the datetime ‘2021-09-30 23:59:59’ using the QUARTER()
function.
SELECT QUARTER('2021-09-30 23:59:59') AS quarter;
The output is:
+---------+
| quarter |
+---------+
| 3 |
+---------+
The output shows that the datetime ‘2021-09-30 23:59:59’ is in the third quarter of the year, as it is in the month of September.
Example 3: Finding the quarter of the current date
The following example finds the quarter of the current date using the QUARTER()
function and the CURDATE()
function. The CURDATE()
function returns the current date as a date value.
SELECT QUARTER(CURDATE()) AS quarter;
The output is:
+---------+
| quarter |
+---------+
| 1 |
+---------+
The output shows that the current date is in the fourth quarter of the year, as it is in the month of October.
Related Functions
There are some other functions that are related to the QUARTER()
function, such as:
MONTH()
: This function returns the month of the year for a given date. The month of the year is a number from 1 to 12, where 1 represents January, 2 represents February, and so on. The syntax of the function isMONTH(date)
, wheredate
is a date or datetime expression. The function returns an integer that represents the month of the year for the given date.YEAR()
: This function returns the year for a given date. The year is a four-digit number, such as 2021, 2020, etc. The syntax of the function isYEAR(date)
, wheredate
is a date or datetime expression. The function returns an integer that represents the year for the given date.DATE_FORMAT()
: This function returns a formatted string representation of a date or datetime value. The format of the string is specified by a format string that contains various specifiers for the date or datetime components, such as %Y for the year, %m for the month, %d for the day, %H for the hour, %i for the minute, %s for the second, etc. The syntax of the function isDATE_FORMAT(date, format)
, wheredate
is a date or datetime expression andformat
is a string that specifies the format. The function returns a string that represents the formatted date or datetime value. For example,DATE_FORMAT('2021-10-15', '%Y-%m-%d')
returns ‘2021-10-15’.
Conclusion
The QUARTER()
function is a useful function to return the quarter of the year for a given date. The quarter of the year is a number from 1 to 4, where 1 represents the first quarter (January to March), 2 represents the second quarter (April to June), 3 represents the third quarter (July to September), and 4 represents the fourth quarter (October to December). The function can be used to perform various calculations and analysis involving quarters, such as grouping, filtering, or aggregating data by quarters. The function takes one argument, which is a date or datetime expression that represents the date for which the quarter is to be returned. The function returns an integer that represents the quarter of the year for the given date. The function can also be combined with other date and time functions, such as MONTH()
, YEAR()
, DATE_FORMAT()
, etc., to perform more complex operations on dates and datetimes.