Skip to main content

month

1. MONTH Function

The MONTH function in H2 database is used to extract the month component from a given date or timestamp.

2. Syntax

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

MONTH(date)

Arguments

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

Return

  • The MONTH function returns an integer value representing the month component of the given date or timestamp.

3. Notes

  • The MONTH function in H2 database returns the month component as an integer value ranging from 1 to 12.
  • If the input date or timestamp is not in a valid format, the MONTH function will return NULL.
  • Ensure that the input date or timestamp is of the correct format and datatype while using the MONTH function to avoid errors.

4. Examples

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

Example 1 - Extracting the month component from a date:

SELECT MONTH(DATE '2022-01-15') AS month;

Output:

month
-----
1

Example 2 - Extracting the month component from a timestamp:

SELECT MONTH(TIMESTAMP '2022-02-25 10:30:00') AS month;

Output:

month
-----
2

Example 3 - Extracting the month component from a column:

CREATE TABLE events (
id INT PRIMARY KEY,
event_date DATE
);

INSERT INTO events VALUES (1, DATE '2022-03-10'), (2, DATE '2022-04-20'), (3, DATE '2022-05-05');

SELECT id, event_date, MONTH(event_date) AS month
FROM events;

Output:

id | event_date  | month
---+-------------+-------
1 | 2022-03-10 | 3
2 | 2022-04-20 | 4
3 | 2022-05-05 | 5
  • year - Extract the year component from a date or timestamp.
  • DAY - Extract the day component from a date or timestamp.