<<<<<<< HEAD year | H2 | SQLDoc
Skip to main content

year

1. YEAR Function

======= year | H2 Function | SQLDoc

year

1. YEAR Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The YEAR function in H2 database is used to extract the year from a given date or timestamp value.

2. Syntax

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

YEAR(date)

Arguments

  • date: The date or timestamp value from which the year needs to be extracted. It can be a column name, a variable, or a literal value of type DATE or TIMESTAMP.

Return

  • The YEAR function returns the year as an integer value.

3. Notes

  • The YEAR function in H2 database extracts the year from a given date or timestamp value. If the input is not a valid date or timestamp, the function will return NULL.
  • Make sure to provide a valid date or timestamp value as the argument for the YEAR function to avoid errors.
  • The YEAR function is specific to H2 database and may have a different implementation or syntax in other database systems.

4. Examples

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

Example 1 - Extracting the year from a date:

SELECT YEAR(DATE '2022-06-15') AS extracted_year;

Output:

extracted_year
--------------
2022

Example 2 - Extracting the year from a timestamp:

SELECT YEAR(TIMESTAMP '2022-06-15 09:30:45') AS extracted_year;

Output:

extracted_year
--------------
2022

Example 3 - Extracting the year from a column:

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

INSERT INTO events VALUES (1, DATE '2022-01-15'), (2, DATE '2022-02-20'), (3, DATE '2022-03-25');

SELECT id, event_date, YEAR(event_date) AS extracted_year
FROM events;

Output:

id | event_date  | extracted_year
---+-------------+----------------
1 | 2022-01-15 | 2022
2 | 2022-02-20 | 2022
3 | 2022-03-25 | 2022
  • month - Extract the month from a date or timestamp
  • DAY - Extract the day from a date or timestamp