MySQL Hour function is one of the Date Functions, which returns the Hour from the given time. This MySQL Hour function returns ta value from 0 to 23. However, if the time is huge, the function return value greater than 23.
The syntax of the Hour Function is as shown below:
HOUR(Time or DateTime expression);
MySQL Hour function Example
These examples help you understand the use of hour function. Here, we are returning the hours from different times and a DateTime expression.
SELECT HOUR('11:12:03');
SELECT HOUR('23:12:33');
SELECT HOUR('2019-01-12 03:12:33');
OUTPUT
Let us see another example of the Hour Date Function. Here, we are using NOW, UTC_TIME, and UTC_TIMESTAMP inside this Hour function to return the Hour from the current date and time.
SELECT HOUR(NOW());
SELECT HOUR(UTC_TIME());
SELECT HOUR(UTC_TIMESTAMP());
OUTPUT
Hour Function Example 2
The following MySQL queries show you what happens when we try to return the hours from string DateTime, invalid time, or 0 as the time value.
SELECT HOUR(NOW() + 15);
SELECT HOUR('10:12:87');
SELECT HOUR('00:00:00');
OUTPUT