MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL HOUR() Function



The MySQL HOUR() function returns the hour portion of a given time or datetime value. Since a time value can range from '-838:59:59' to '838:59:59', therefore this function can return value between 0 to 838 (higher than 23).

Syntax

HOUR(datetime)

Parameters

datetime Required. Specify a time or datetime value from which to extract the hour.

Return Value

Returns the hour part of a given time or datetime value.

Example 1:

The example below shows the usage of HOUR() function.

mysql> SELECT HOUR('2018-08-18');
Result: 0

mysql> SELECT HOUR('2018-08-18 10:38:42');
Result: 10

mysql> SELECT HOUR('2018-08-18 10:38:42.000004');
Result: 10

mysql> SELECT HOUR("838:38:42");
Result: 838

mysql> SELECT HOUR(CURDATE());
Result: 11

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the hour portion of records of column OrderTime:

SELECT *, HOUR(OrderTime) AS HOUR_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeHOUR_Value
1001.582017-08-18 10:38:4210
1201.612018-03-23 07:14:167
1251.782018-09-12 05:25:565
501.802019-01-16 11:52:0511
2001.722020-02-06 09:31:349

❮ MySQL Functions