Function Reference

HOUR

PostgreSQL equivalent:

Returns the hours of a time.

MySQL 8.1
HOUR(time)

Parameters

timeTIME
Time or datetime

Return value

An integer from 0 to 23; up to 838 for a TIME value; NULL if the value is not recognized

Examples

Hour from a date and time

MySQL 8.1
SELECT HOUR('2022-06-16 16:37:03');
HOUR('2022-06-16 16:37:03')
16

Departure hour of trips

MySQL 8.1
SELECT id,
	town_from,
	town_to,
	HOUR(time_out) AS departure_hour
FROM Trip;
idtown_fromtown_todeparture_hour
1100RostovParis14
1101ParisRostov8
1123RostovVladivostok16
1124VladivostokRostov9
1145MoscowRostov9
1146RostovMoscow17
1181RostovMoscow6
1182MoscowRostov12
1187RostovMoscow15
1188MoscowRostov22

More hours than a day

MySQL 8.1
SELECT HOUR('100:30:00') AS hours;
hours
100

Details

For a TIME value, such as a duration, the hours are not limited to a day: HOUR('100:30:00') returns 100. To get the hours within a day, take the remainder: HOUR(t) % 24.

See also