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.
Syntax:
QUARTER(date);
Where date1 is a date.
Syntax Diagram:

MySQL Version: 5.6
Video Presentation:
Pictorial Presentation:

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');
Sample Output:
mysql> SELECT QUARTER('2009-05-18'); +-----------------------+ | QUARTER('2009-05-18') | +-----------------------+ | 2 | +-----------------------+ 1 row in set (0.01 sec)
PHP script:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-QUARTER-function - php mysql examples | w3resource</title>
<meta name="description" content="example-QUARTER-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Calculating a value between 1 to 4 as a QUARTER of a year for a given date 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated output</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT QUARTER("2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['QUARTER("2009-05-18")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP script:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-quarter-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT QUARTER('2009-05-18')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Calculated output</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("QUARTER('2009-05-18')")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
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
Code:
SELECT invoice_no,ord_date,QUARTER(ord_date)
FROM purchase
WHERE QUARTER(ord_date)=2 ;
Sample 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)
PHP script:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example1-QUARTER-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-QUARTER-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of invoice number, order date and in which QUARTER orders are placed. Order which are placed in the second QUARTER, are listed only:</h2>
<table class='table table-bordered'>
<tr>
<th>Invoice no.</th><th>Order date</th><th>In which QUARTER the order is placed</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT invoice_no,ord_date,QUARTER(ord_date)
FROM purchase
WHERE QUARTER(ord_date)=2') as $row) {
echo "<tr>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['QUARTER(ord_date)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
Online Practice Editor:
All Date and Time Functions:
Click here to see the MySQL Date and time functions.
Previous: PERIOD_DIFF()
Next: SEC_TO_TIME()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises