MySQL HOUR() Function

In MySQL, the HOUR() function extracts and returns the hour portion of a time.

HOUR() Syntax

Here is the syntax of MySQL HOUR() function:

HOUR(time)

Parameters

time
Required. A time or datetime expression.

Return value

The MySQL HOUR() function extracts the hour part of a specified time or datetime and returns it as a number.

  • If there is no hour part in time, the HOUR() function will return 0.
  • Tf the argument is NULL, The HOUR() function will return NULL.
  • Since MySQL supports a maximum time of 838:59:59, the maximum number returned the HOUR() function is 838.

HOUR() Examples

Here are some examples of the HOUR() function.

SELECT
    HOUR('10:10:10'),
    HOUR('2022-02-28 10:10:10'),
    HOUR('123:10:10'),
    HOUR('1234:10:10'),
    HOUR('2022-02-00'),
    HOUR('2022-02-30'),
    HOUR('Not A DATE'),
    HOUR(NULL)\G
           HOUR('10:10:10'): 10
HOUR('2022-02-28 10:10:10'): 10
          HOUR('123:10:10'): 123
         HOUR('1234:10:10'): 838
         HOUR('2022-02-00'): 0
         HOUR('2022-02-30'): 0
         HOUR('Not A DATE'): NULL
                 HOUR(NULL): NULL

Here:

  • HOUR('123:10:10') returns 123, HOUR('1234:10:10') returns 838. This is because the maximum time is 838:59:59.
  • Although '2022-02-00' and '2022-02-30are incorrect times, the HOUR() function still returns 0.