w3resource

MySQL QUARTER() function

QUARTER() function

MySQL QUARTER() returns the quarter of the year for a date. The return value is in the range of 1 to 4. It provides a way to identify the quarter in which a date falls, facilitating various date-related calculations and data categorization.

This function is useful in -

  • QUARTER() is essential for identifying the season or part of the year in which an event or activity occurs.
  • The function is valuable for tracking progress toward goals set for specific quarters.
  • QUARTER() is used to make seasonal adjustments to data to account for quarter-specific trends.
  • QUARTER() is valuable for categorizing dates into specific quarters, aiding in data organization and analysis.
  • QUARTER() is useful in financial forecasting, where data is projected for specific quarters.
  • The function supports trend analysis by allowing data to be grouped and analyzed by quarters.
  • QUARTER() helps aggregate data by quarter, enabling summary analysis over distinct periods.

Syntax:

QUARTER(date);

Where date1 is a date.

Syntax Diagram:

MySQL QUARTER() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial Presentation:

Pictorial Presentation of MySQL QUARTER() function

Example: MySQL QUARTER() function

The following statement will return a value between 1 to 4 as a QUARTER of a year for a given date 2009-05-18.

Code:

SELECT QUARTER('2009-05-18');

Output:

mysql> SELECT QUARTER('2009-05-18');
+-----------------------+
| QUARTER('2009-05-18') |
+-----------------------+
|                     2 | 
+-----------------------+
1 row in set (0.01 sec)

Example: QUARTER() function using table

The following statement will return a value between 1 to 4 as a QUARTER of a year for ord_date from the table purchase making sure that the ord_date belongs to the 2nd QUARTER.

Sample table: purchase

+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+
| invoice_no | invoice_dt | ord_no         | ord_date   | receive_dt | book_id | book_name                       | pub_lang | cate_id | receive_qty | purch_price | total_cost |
+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+
| INV0001    | 2008-07-15 | ORD/08-09/0001 | 2008-07-06 | 2008-07-19 | BK001   | Introduction to Electrodynamics | English  | CA001   |          15 |       75.00 |    1125.00 |
| INV0002    | 2008-08-25 | ORD/08-09/0002 | 2008-08-09 | 2008-08-28 | BK004   | Transfer  of Heat and Mass      | English  | CA002   |           8 |       55.00 |     440.00 |
| INV0003    | 2008-09-20 | ORD/08-09/0003 | 2008-09-15 | 2008-09-23 | BK005   | Conceptual Physics              | NULL     | CA001   |          20 |       20.00 |     400.00 |
| INV0004    | 2007-08-30 | ORD/07-08/0005 | 2007-08-22 | 2007-08-30 | BK004   | Transfer  of Heat and Mass      | English  | CA002   |          15 |       35.00 |     525.00 |
| INV0005    | 2007-07-28 | ORD/07-08/0004 | 2007-06-25 | 2007-07-30 | BK001   | Introduction to Electrodynamics | English  | CA001   |           8 |       25.00 |     200.00 |
| INV0006    | 2007-09-24 | ORD/07-08/0007 | 2007-09-20 | 2007-09-30 | BK003   | Guide to Networking             | Hindi    | CA003   |          20 |       45.00 |     900.00 |
+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+

Code:

SELECT invoice_no,ord_date,QUARTER(ord_date)
FROM purchase        
WHERE QUARTER(ord_date)=2 ;

Output:

mysql> SELECT invoice_no,ord_date,QUARTER(ord_date)
    -> FROM purchase        
    -> WHERE QUARTER(ord_date)=2 ;
+------------+------------+-------------------+
| invoice_no | ord_date   | QUARTER(ord_date) |
+------------+------------+-------------------+
| INV0005    | 2007-06-25 |                 2 | 
+------------+------------+-------------------+
1 row in set (0.00 sec)

Video Presentation:

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

PREV : PERIOD_DIFF()
NEXT : SEC_TO_TIME()



Follow us on Facebook and Twitter for latest update.