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

array_slice

1. ARRAY_SLICE Function

======= array_slice | H2 Function | SQLDoc

array_slice

1. ARRAY_SLICE Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The ARRAY_SLICE function in H2 database is used to extract a portion of an array.

2. Syntax

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

ARRAY_SLICE(array, start, length)

Arguments

  • array: The array from which a portion needs to be extracted. It should be an array or a column of an array data type.
  • start: The starting index of the portion to be extracted. It should be an integer value.
  • length: The length of the portion to be extracted. It should be an integer value.

Return

  • The ARRAY_SLICE function returns a new array that contains the portion extracted from the original array.

3. Notes

  • The ARRAY_SLICE function in H2 database expects the array to be of a valid array data type, such as ARRAY, INTARRAY, VARCHARARRAY, etc.
  • The start index should be a non-negative integer. If the start index is negative or exceeds the array size, an exception will be thrown.
  • If the length is negative, the function will return an empty array.
  • If the length exceeds the remaining length of the array from the start index, the function will return the portion until the end of the array.

4. Examples

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

Example 1 - Extracting a portion of an array:

SELECT ARRAY_SLICE(ARRAY[1, 2, 3, 4, 5], 2, 3) AS sliced_array;

Output:

sliced_array
---------------
[3, 4, 5]

Example 2 - Extracting a portion of an array stored in a column:

CREATE TABLE arrays (
id INT PRIMARY KEY,
values INTARRAY
);

INSERT INTO arrays VALUES (1, ARRAY[1, 2, 3, 4, 5]), (2, ARRAY[10, 20, 30, 40, 50]);

SELECT id, ARRAY_SLICE(values, 1, 3) AS sliced_array
FROM arrays;

Output:

id | sliced_array
---+---------------
1 | [2, 3, 4]
2 | [20, 30, 40]
  • ARRAY_LENGTH - Get the length of an array
  • array_contains - Check if an array contains a specific value