Skip to main content

quarter

1. QUARTER Function

The QUARTER function in H2 database is used to extract the quarter from a given date.

2. Syntax

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

QUARTER(date)

Arguments

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

Return

  • The QUARTER function returns the quarter of the year as an integer value (ranging from 1 to 4) based on the provided date.

3. Notes

  • The QUARTER function in H2 database considers January to March as the first quarter, April to June as the second quarter, July to September as the third quarter, and October to December as the fourth quarter.
  • If the input date is not a valid date or timestamp, the QUARTER function will return NULL.
  • Ensure that you provide the correct date format to the QUARTER function to avoid errors.

4. Examples

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

Example 1 - Extracting the quarter from a specific date:

SELECT QUARTER(DATE '2022-06-15') AS quarter;

Output:

quarter
-------
2

Example 2 - Extracting the quarter from a column of dates:

CREATE TABLE sales (
id INT PRIMARY KEY,
order_date DATE
);

INSERT INTO sales VALUES (1, DATE '2022-02-10'), (2, DATE '2022-07-25'), (3, DATE '2022-11-30');

SELECT id, order_date, QUARTER(order_date) AS quarter
FROM sales;

Output:

id | order_date  | quarter
---+-------------+--------
1 | 2022-02-10 | 1
2 | 2022-07-25 | 3
3 | 2022-11-30 | 4
  • year - Extract the year from a date
  • month - Extract the month from a date
  • DAY - Extract the day from a date