MySQL HOUR() Function

The HOUR() function extracts the hour part from a given DATETIME, DATE, or TIME value.


Definition and Usage

  • The HOUR() function returns the hour part of a given date or time value.
  • The value is returned as an integer from 0 to 838 (representing the hour part of the TIME or DATETIME).

Syntax

HOUR(datetime)
  • datetime: Required. The DATETIME, DATE, or TIME value from which the hour part is to be extracted.

Examples

Example 1: Extract the hour part from a DATETIME

SELECT HOUR("2017-06-20 09:34:00");

Output:

9

This example extracts the hour 9 from the DATETIME value 2017-06-20 09:34:00.

Example 2: Extract the hour part from a TIME

SELECT HOUR("838:59:59");

Output:

838

This example extracts the hour part 838 from the TIME value 838:59:59.


Technical Details

  • Works in MySQL versions: From MySQL 4.0.
  • The HOUR() function can be used with both DATETIME and TIME types.

Was this article helpful?