×

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples.

Introduction of MySQL QUARTER() function

The QUARTER() function in MySQL returns the Quarter of the year for a given date value. It returns a number from 1 to 4.

Syntax of the MySQL QUARTER() function

The syntax of the MySQL QUARTER() function is given as follows:

QUARTER(dt)

Parameters or arguments used in MySQL QUARTER() function:

There is only one parameter accepted by the QUARTER() function in MySQL, which is given as follows:

DT: The date or DateTime from which we want to extract the Quarter in MySQL.

Returns:

It will return 1 if the given date is in the range January-March, return 2 for April-June, return 3 for July-September, and if the date is in the range from October-December, it returns 4.

Application used for QUARTER() function:

The QUARTER() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL QUARTER() function:

Now we will look into some MySQL QUARTER() function examples and will explore how we can use the QUARTER function in MySQL.

Example-1 :

In this example, we will find the Current QUARTER Using the QUARTER() Function in MySQL.

SELECT QUARTER(NOW()) AS CURRENT_QUARTER;

Output:

CURRENT_QUARTER
3
1 row in set (0.00 sec)

Example-2 :

In this example, we will find the Quarter from the given DateTime Using the QUARTER() Function.

SELECT QUARTER('2020-03-26 08:09:22') AS QUARTER_NUMBER;

Output:

QUARTER_NUMBER
1
1 row in set (0.00 sec)

Example-3 :

In this example, we will find the Quarter from the given DateTime Using the QUARTER() Function when the date is NULL.

SELECT QUARTER(NULL) AS QUARTER_NUMBER;

Output:

QUARTER_NUMBER
NULL
1 row in set (0.00 sec)

Example-4:

In this example we will create a table named Commodity which allows you to perform the quarter function on Selling_Date.

CREATE TABLE Commodity(
Commodity_id INT AUTO_INCREMENT,
Commodity_name VARCHAR(100) NOT NULL,
Purchasing_price DECIMAL(13, 2) NOT NULL,
Selling_price DECIMAL(13, 2) NOT NULL,
Selling_Date Date NOT NULL,
PRIMARY KEY(Commodity_id)
);

Now, we will insert some data to the Commodity table.

INSERT INTO
Commodity(Commodity_name, Purchasing_price, Selling_price, Selling_Date)
VALUES
('Audi Q8', 10000000.00, 15000000.00, '2018-01-26' ),
('Volvo XC40', 2000000.00, 3000000.00, '2018-04-20' ),
('Audi A6', 4000000.00, 5000000.00, '2018-07-25' ),
('BMW X5', 5000500.00, 7006500.00, '2018-10-18' ),
('Jaguar XF', 5000000, 7507000.00, '2018-01-27' ),
('Mercedes-Benz C-Class', 4000000.00, 6000000.00, '2018-04-01'
),
('Jaguar F-PACE,' 5000000.00, 7000000.00, '2018-12-26' ),
('Porsche Macan', 6500000.00, 8000000.00, '2018-04-16' ) ;

So, Our table looks like this:

Commodity_idCommodity_namePurchasing_priceSelling_priceSelling_Date
1Audi Q810000000.0015000000.002018-01-26
2Volvo XC402000000.003000000.002018-04-20
3Audi A64000000.005000000.002018-07-25
4BMW X55000500.007006500.002018-10-18
5Jaguar XF5000000.007507000.002019-01-27
6Mercedes-Benz C-Class4000000.006000000.002019-04-01
7Jaguar F-PACE5000000.007000000.002019-12-26
8Porsche Macan6500000.008000000.002020-04-16

We will find the number of Commodities sold per Quarter using the MONTH () function.

SELECT
QUARTER(Selling_Date) as quarter,
COUNT(Commodity_id) as COMMODITY_SOLD
FROM
Commodity
GROUP BY QUARTER(Selling_Date)
ORDER BY QUARTER(Selling_Date);

Output :

QUARTERCOMMODITY_SOLD
12
23
31
42

Application of MySQL QUARTER() function:

This function is used to return the Quarter of the year for a given date value.

Summary:

In the above context, we have learned how to use the QUARTER() function in MySQL to return the Quarter of the year for a given date value.


Related Topics

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL NOW() function

In this context, we will learn how we can use the MySQL NOW() function with proper syntax and good examples. Introduction of MySQL NOW() function Basically, in MySQL, this function is used...

2 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.