w3resource

MySQL YEAR() function

YEAR() function

MySQL YEAR() returns the year for a given date. The return value is in the range of 1000 to 9999 or 0 for 'zero' date.

Syntax:

YEAR(dt)

Where dt is a date.

Syntax Diagram:

MySQL YEAR() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:

Pictorial Presentation:

Pictorial Presentation of MySQL YEAR() function

Example: MySQL YEAR() function

The following statement will return the year part of the specified date 2009-05-19.

Code:

SELECT YEAR('2009-05-19');

Sample Output:

mysql> SELECT YEAR('2009-05-19');
+--------------------+
| YEAR('2009-05-19') |
+--------------------+
|               2009 | 
+--------------------+
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>example-year-function - php mysql examples | w3resourc</title>
<meta name="description" content="example-year-function - php mysql examples | w3resourc">
<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>Year from 2009-05-19:</h2>
<table class='table table-bordered'>
<tr>
<th>Year from 2009-05-19</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 YEAR("2009-05-19")') as $row) {
echo "<tr>";
echo "<td>" . $row['YEAR("2009-05-19")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

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-year-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 YEAR('2009-05-19')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Year from 2009-05-19</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("YEAR('2009-05-19')")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: YEAR() function using table

The following statement will retrieve the columns 'book_name', 'dt_of_pub' and year of publication from book_mast table for those rows whose year of 'dt_of_pub' are after 2003.

Sample table: book_mast


Code:

SELECT book_name,dt_of_pub,YEAR(dt_of_pub)
FROM book_mast
WHERE YEAR(dt_of_pub)>2003;

Sample Output:

mysql> SELECT book_name,dt_of_pub,YEAR(dt_of_pub)
    -> FROM book_mast
    -> WHERE YEAR(dt_of_pub)>2003; 
+----------------------------------+------------+-----------------+
| book_name                        | dt_of_pub  | YEAR(dt_of_pub) |
+----------------------------------+------------+-----------------+
| Transfer  of Heat and Mass       | 2004-02-16 |            2004 | 
| Advanced 3d Graphics             | 2004-02-16 |            2004 | 
| Mental Health Nursing            | 2004-02-10 |            2004 | 
| The Experimental Analysis of Cat | 2007-06-09 |            2007 | 
| The Nature  of World             | 2005-12-20 |            2005 | 
+----------------------------------+------------+-----------------+
5 rows in set (0.06 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-year-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-year-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 books with their date and year of publication:</h2>
<table class='table table-bordered'>
<tr> 
<th>Name of the Book</th><th>Date of publication</th><th>Year of publication</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 book_name,dt_of_pub,YEAR(dt_of_pub)
FROM book_mast
WHERE YEAR(dt_of_pub)>2003') as $row) {
echo "<tr>"; 
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['dt_of_pub'] . "</td>"; 
echo "<td>" . $row['YEAR(dt_of_pub)'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Online Practice Editor:


All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: WEEK OF YEAR()
Next: YEARWEEK()



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook