MySQL HOUR() function
HOUR() function
MySQL HOUR() returns the HOUR of a time. The return value is within the range of 0 to 23 for time-of-day values. The range of time values may be larger than 23.
Syntax:
HOUR(time)
Where time is a time.
Syntax Diagram:

MySQL Version: 5.6
Video Presentation:
Pictorial Presentation:

Example: MySQL HOUR() function
If we get to return the HOUR from the given time 15:13:46, the following SQL can be used -
Code:
SELECT HOUR('15:13:46');
Sample Output:
mysql> SELECT HOUR('15:13:46'); +------------------+ | HOUR('15:13:46') | +------------------+ | 15 | +------------------+ 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-HOUR-function - php mysql examples | w3resource</title>
<meta name="description" content="example-HOUR-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>Extract HOUR from 15:13:46:</h2>
<table class='table table-bordered'>
<tr>
<th>HOUR from 15:13:46</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 HOUR("15:13:46")') as $row) {
echo "<tr>";
echo "<td>" . $row['HOUR("15:13:46")'] . "</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-hour-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 HOUR('15:13:46')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>HOUR from 15:13:46</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("HOUR('15:13:46')")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Sample table: wages_emp
+--------+----------+------------+-----------+----------+---------+-------+ | emp_id | emp_name | wor_dt | time_from | time_to | hr_rate | wages | +--------+----------+------------+-----------+----------+---------+-------+ | E001 | RAMON | 2010-02-10 | 10:00:00 | 14:00:00 | 50 | 200 | | E002 | SILTON | 2010-05-05 | 11:00:00 | 16:00:00 | 60 | 300 | | E001 | RAMON | 2010-07-11 | 11:00:00 | 16:00:00 | 60 | 300 | | E001 | RAMON | 2011-09-15 | 08:00:00 | 12:00:00 | 75 | 300 | +--------+----------+------------+-----------+----------+---------+-------+
MySQL HOUR() function with WHERE clause
If we want to know the information for the worker who starts the work after 10 a.m, the following sql can be used -
Code:
SELECT * FROM wages_emp
WHERE HOUR(time_from)>10;
Sample Output:
+--------+----------+------------+-----------+----------+---------+-------+ | emp_id | emp_name | wor_dt | time_from | time_to | hr_rate | wages | +--------+----------+------------+-----------+----------+---------+-------+ | E002 | SILTON | 2010-05-05 | 11:00:00 | 16:00:00 | 60 | 300 | | E001 | RAMON | 2010-07-11 | 11:00:00 | 16:00:00 | 60 | 300 | +--------+----------+------------+-----------+----------+---------+-------+ 2 rows in set (0.00 sec)
MySQL HOUR() function with BETWEEN
If we want to know the information for the workers who works for a duration of 2 to 4 hours, the following sql can be used -
Code:
SELECT emp_name,wor_dt,time_from,time_to,
HOUR(time_to)-HOUR(time_from) as duration,wages
FROM wages_emp
WHERE HOUR(time_to)-HOUR(time_from)
BETWEEN 2 AND 4;
Sample Output:
+----------+------------+-----------+----------+----------+-------+ | emp_name | wor_dt | time_from | time_to | duration | wages | +----------+------------+-----------+----------+----------+-------+ | RAMON | 2010-02-10 | 10:00:00 | 14:00:00 | 4 | 200 | | RAMON | 2011-09-15 | 08:00:00 | 12:00:00 | 4 | 300 | +----------+------------+-----------+----------+----------+-------+ 2 rows in set (0.00 sec)
MySQL HOUR() function with IN
If we want to know the information for the workers who usually starts work at 8 or 9 or 11 a.m. to their working days, the following sql can be used -
Code:
SELECT *
FROM wages_emp
WHERE HOUR(time_from)
IN(8,9,11);
Sample Output:
+--------+----------+------------+-----------+----------+---------+-------+ | emp_id | emp_name | wor_dt | time_from | time_to | hr_rate | wages | +--------+----------+------------+-----------+----------+---------+-------+ | E002 | SILTON | 2010-05-05 | 11:00:00 | 16:00:00 | 60 | 300 | | E001 | RAMON | 2010-07-11 | 11:00:00 | 16:00:00 | 60 | 300 | | E001 | RAMON | 2011-09-15 | 08:00:00 | 12:00:00 | 75 | 300 | +--------+----------+------------+-----------+----------+---------+-------+ 3 rows in set (0.00 sec)
All Date and Time Functions:
Click here to see the MySQL Date and time functions.
Previous: GET_FORMAT()
Next: LAST_DAY()
- 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
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