Skip to main content

hour

1. HOUR Function

The HOUR function in H2 database is used to extract the hour component from a given time or timestamp value.

2. Syntax

The syntax for the HOUR function in H2 database is as follows:

HOUR(time)

Arguments

  • time: The time or timestamp value from which the hour component needs to be extracted. It should be a valid time or timestamp value.

Return

  • The HOUR function returns the hour component of the given time or timestamp value as an integer.

3. Notes

  • The HOUR function in H2 database extracts the hour value from the time or timestamp value, ranging from 0 to 23.
  • If the input value is not a valid time or timestamp, the HOUR function will return NULL.
  • Ensure that the input value is in the correct format and datatype to avoid errors while using the HOUR function.

4. Examples

Here are a few examples demonstrating the usage of the HOUR function in H2 database:

Example 1 - Extracting the hour from a time value:

SELECT HOUR(TIME '10:30:45') AS hour;

Output:

hour
----
10

Example 2 - Extracting the hour from a timestamp value:

SELECT HOUR(TIMESTAMP '2022-01-01 18:45:30') AS hour;

Output:

hour
----
18

Example 3 - Extracting the hour from a column:

CREATE TABLE events (
id INT PRIMARY KEY,
event_time TIMESTAMP
);

INSERT INTO events VALUES (1, TIMESTAMP '2022-01-01 12:30:00'), (2, TIMESTAMP '2022-01-01 23:15:00');

SELECT id, event_time, HOUR(event_time) AS hour
FROM events;

Output:

id |         event_time         | hour
---+----------------------------+------
1 | 2022-01-01 12:30:00.000000 | 12
2 | 2022-01-01 23:15:00.000000 | 23
  • minute - Extract the minute component from a time or timestamp value.
  • second - Extract the second component from a time or timestamp value.