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

array_get

1. ARRAY_GET Function

======= array_get | H2 Function | SQLDoc

array_get

1. ARRAY_GET Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The ARRAY_GET function in H2 database is used to retrieve the element at a specified index from an array.

2. Syntax

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

ARRAY_GET(array, index)

Arguments

  • array: The array from which the element needs to be retrieved. It should be an array or a list of values.
  • index: The index of the element to be retrieved. It should be a numeric value representing the zero-based index.

Return

  • The ARRAY_GET function returns the element at the specified index from the array. The datatype of the returned value depends on the datatype of the array elements.

3. Notes

  • The ARRAY_GET function in H2 database supports arrays of any type, including numeric, string, and date types.
  • The index specified should be within the bounds of the array. If the index is negative or exceeds the array size, the function will return NULL.
  • If the input array is NULL, the function will also return NULL.
  • Make sure to use the correct syntax and data types while using the ARRAY_GET function to avoid errors.

4. Examples

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

Example 1 - Retrieving an element from an array:

SELECT ARRAY_GET(ARRAY[10, 20, 30, 40], 2) AS element;

Output:

element
-------
30

Example 2 - Retrieving an element from an array stored in a column:

CREATE TABLE data (
id INT PRIMARY KEY,
values ARRAY
);

INSERT INTO data VALUES (1, ARRAY['apple', 'banana', 'cherry']), (2, ARRAY[1, 2, 3, 4]);

SELECT id, values, ARRAY_GET(values, 1) AS element
FROM data;

Output:

id | values                 | element
---+------------------------+---------
1 | ['apple','banana','cherry'] | banana
2 | [1,2,3,4] | 2
  • ARRAY_LENGTH - Get the length of an array
  • array_contains - Check if an array contains a specific value