MySQL HOUR() Function
The HOUR()
function in MySQL extracts the hour from a time or datetime value. This is a very useful function for working with time data and for performing time-based analysis or filtering.
HOUR(): Definition and Usage
HOUR()
is very handy for isolating the hour portion of a timestamp. You might use it to analyze events based on the hour they occurred, to create detailed time-based reports, or to filter data based on specific hourly intervals. The function returns an integer between 0 and 838, representing the hour. If the input is not a valid time or datetime, it returns 0.
Syntax
Syntax
HOUR(datetime)
Parameter Values
Parameter | Description |
---|---|
datetime |
The time or datetime value. This is required. |
Examples
Extracting the Hour from a DATETIME Value
This example extracts the hour (9) from the datetime value "2017-06-20 09:34:00".
Syntax
SELECT HOUR("2017-06-20 09:34:00");
Output
9
Extracting the Hour from a TIME Value
This example shows how to extract the hour from a time value that exceeds 24 hours.
Syntax
SELECT HOUR("838:59:59");
Output
11