MySQL Hour() Function

Last Updated : 3 Feb 2026

The hour is a MySQL date/time function. It is used to get the hour from the given datetime.

It returns an integer value ranging from 0 to 23. In this article, we will understand the concept of MySQL hour() function in detail with its various examples.

Syntax

Parameter:

Datetime: datetime value for getting hour.

Returns:

This function returns the hour portion of a date value.

Example 1

Explanation: While executing the above query, the hour() function extracts the hours from the given date and time.

Output: The result of the example is given below.

MySQL Hour() Function

Example 2

Explanation: While executing the above query, the hour() function extracts the hours from the given date and time.

Output: The result of the example is given below.

MySQL Hour() Function

Example 3:

Explanation: While executing the above query, the hour() function extracts the hours from the current date and time.

Output: The result of the example is given below.

Result
07

Using MySQL DateTime hour function in a Table column

We can use the MySQL DateTime hour() function in a table to extracts the hours from the given date/time and time table column.

Following the steps below to perform the functionality of MySQL hour() function in a column of the table.

Step 1: Create a table named Order_Details using the CREATE TABLE statement.

Step 2: After that INSERT data into a table using the INSERT TABLE statement.

Step 3: To check the information with the use of SELECT statement, whether the data is inserted or not.

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
2Priyanka SharmaAccessories2025-10-08 15:10:00500
3Bobby KapoorFood2025-10-23 16:00:00800
4Alisha SharmaFinance2025-11-20 05:10:00600
5Eshant SharmaClothes2025-11-30 08:10:001000
6Ravi RathoreFood2025-05-19 07:20:001450
7Ravi SharmaSanitary2026-01-01 01:40:002500
8Sanjay SharmaAccessories2026-01-03 11:10:001000

Example:

Explanation: While executing the above query, the hour() function is used to extract the time from a Order_Schduled column in the Order_Details table.

Output: The result of the example is given below.

OIDNameDepartmentOrder_SchduledAmountResult
1Anjana GuptaClothes2025-07-25 10:20:00150010
2Priyanka SharmaAccessories2025-10-08 15:10:0050015
3Bobby KapoorFood2025-10-23 16:00:0080016
4Alisha SharmaFinance2025-11-20 05:10:0060005
5Eshant SharmaClothes2025-11-30 08:10:00100008
6Ravi RathoreFood2025-05-19 07:20:00145007
7Ravi SharmaSanitary2026-01-01 01:40:00250001
8Sanjay SharmaAccessories2026-01-03 11:10:00100011

Using MySQL DateTime hour function in a Table with WHERE Clause

We can use the MySQL DateTime hour() function in a table to return the hours from the table column. By using WHERE clause we can restrict the data.

Sample Table: Order_Details

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
2Priyanka SharmaAccessories2025-10-08 15:10:00500
3Bobby KapoorFood2025-10-23 16:00:00800
4Alisha SharmaFinance2025-11-20 05:10:00600
5Eshant SharmaClothes2025-11-30 08:10:001000
6Ravi RathoreFood2025-05-19 07:20:001450
7Ravi SharmaSanitary2026-01-01 01:40:002500
8Sanjay SharmaAccessories2026-01-03 11:10:001000

Example:

Explanation: While executing the above query, the hour() function is used to extract the hour from a Order_Date column in the Order_Details table Where the value of the Amount column is greater than 600.

Output: The result of the example is given below.

OIDNameDepartmentOrder_SchduledAmountResult
1Anjana GuptaClothes2025-07-25 10:20:00150010
3Bobby KapoorFood2025-10-23 16:00:0080016
5Eshant SharmaClothes2025-11-30 08:10:00100008
6Ravi RathoreFood2025-05-19 07:20:00145007
7Ravi SharmaSanitary2026-01-01 01:40:00250001
8Sanjay SharmaAccessories2026-01-03 11:10:00100011

Using MySQL DateTime hour function with IN Operator

We can use the MySQL datetime hour() function in a table to retrieve the hours from a table column. The IN operator is used to check whether values in the table are present in the list of values defined in the MySQL statement.

Sample Table: Order_Details

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
2Priyanka SharmaAccessories2025-10-08 15:10:00500
3Bobby KapoorFood2025-10-23 16:00:00800
4Alisha SharmaFinance2025-11-20 05:10:00600
5Eshant SharmaClothes2025-11-30 08:10:001000
6Ravi RathoreFood2025-05-19 07:20:001450
7Ravi SharmaSanitary2026-01-01 01:40:002500
8Sanjay SharmaAccessories2026-01-03 11:10:001000

Example:

Explanation: In the above select query, the hour() function is used to extract the time value from the Order_Scheduled column in the Order_Details table, where the value of the Order_Scheduled column is either 05, 10, or 11.

Output: The result of the example is given below.

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
4Alisha SharmaFinance2025-11-20 05:10:00600
8Sanjay SharmaAccessories2026-01-03 11:10:001000

Using MySQL DateTime hour function with BETWEEN Operator

We can use the MySQL DateTime hour() function in a table to extract the hours. The MYSQL BETWEEN condition checks whether a value is within a range or not.

Sample Table: Order_Details

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
2Priyanka SharmaAccessories2025-10-08 15:10:00500
3Bobby KapoorFood2025-10-23 16:00:00800
4Alisha SharmaFinance2025-11-20 05:10:00600
5Eshant SharmaClothes2025-11-30 08:10:001000
6Ravi RathoreFood2025-05-19 07:20:001450
7Ravi SharmaSanitary2026-01-01 01:40:002500
8Sanjay SharmaAccessories2026-01-03 11:10:001000

Example:

Explanation: While executing the above query, the hour() function is used to extract the time value from an Order_Schduled column in the Order_Details table where the value of the Order_Schduled column is between 05 and 10.

Output: The result of the example is given below.

OIDNameDepartmentOrder_SchduledAmount
1Anjana GuptaClothes2025-07-25 10:20:001500
4Alisha SharmaFinance2025-11-20 05:10:00600
5Eshant SharmaClothes2025-11-30 08:10:001000
6Ravi RathoreFood2025-05-19 07:20:001450
7Ravi SharmaSanitary2026-01-01 01:40:002500

Next TopicMySQL date/time