HOUR

The HOUR function returns the hour part of a value.

Read syntax diagramSkip visual syntax diagramHOUR (expression)
expression
An expression that returns a value of one of the following built-in data types: a date, a time, a timestamp, a character string, a graphic string, or a numeric data type.
  • If expression is a character or graphic string, its value must be a valid string representation of a datetime value. If expression is a valid string representation of a date, it must be in one of the IBM® SQL standard formats. For the valid formats of string representations of datetime values, see String representations of datetime values.
  • If the argument is a DATE, it is first converted to a TIMESTAMP(0) value, assuming a time of exactly midnight (00.00.00).
  • If expression is a number, it must be a time duration or timestamp duration. For the valid formats of datetime durations, see Datetime operands and durations.

The result of the function is a large integer. If the argument can be null, the result can be null; if the argument is null, the result is the null value.

The other rules depend on the data type of the argument:

  • If the argument is a date, time, timestamp, or valid character-string representation of a date, time, or timestamp:

    The result is the hour part of the value, which is an integer between 0 and 24.

  • If the argument is a time duration or timestamp duration:

    The result is the hour part of the value, which is an integer between -99 and 99. A nonzero result has the same sign as the argument.

Example

  • Using the CL_SCHED sample table, select all the classes that start in the afternoon.
      SELECT *
        FROM CL_SCHED
        WHERE HOUR(STARTING) BETWEEN 12 AND 17