Skip to main content

last_day

1. LAST_DAY Function

The LAST_DAY function in H2 database is used to retrieve the last day of the month for a given date.

2. Syntax

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

LAST_DAY(date)

Arguments

  • date: The input date for which you want to find the last day of the month. It should be a valid date expression.

Return

  • The LAST_DAY function returns a date value representing the last day of the month for the given input date.

3. Notes

  • The LAST_DAY function in H2 database considers the input date and returns the corresponding last day of the month.
  • If the input date is already the last day of the month, the function will return the same date.
  • If the input date is NULL, the LAST_DAY function will also return NULL.
  • Ensure that the input date is provided in the correct format to avoid errors.

4. Examples

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

Example 1 - Finding the last day of the month for a given date:

SELECT LAST_DAY(DATE '2022-06-15') AS last_day;

Output:

last_day
------------
2022-06-30

Example 2 - Retrieving the last day of the month for dates stored in a column:

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

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

SELECT id, transaction_date, LAST_DAY(transaction_date) AS last_day
FROM sales;

Output:

id | transaction_date |  last_day
---+-----------------+------------
1 | 2022-01-05 | 2022-01-31
2 | 2022-02-15 | 2022-02-28
3 | 2022-03-20 | 2022-03-31
  • extract - Extract a part of the date
  • dateadd - Add or subtract a specific time interval from a date