Skip to main content

array_cat

1. ARRAY_CAT Function

The ARRAY_CAT function in H2 database is used to concatenate two or more arrays into a single array.

2. Syntax

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

ARRAY_CAT(array1, array2, ...)

Arguments

  • array1, array2, ...: The arrays to be concatenated. Each argument should be an array.

Return

  • The ARRAY_CAT function returns a new array that is the concatenation of the input arrays.

3. Notes

  • The ARRAY_CAT function in H2 database concatenates the arrays in the order they are provided as arguments.
  • If any of the input arrays is NULL, the resulting array will also be NULL.
  • The input arrays should have elements of the same type. If the element types differ, you may need to convert them using appropriate functions like CAST or CONVERT.
  • The ARRAY_CAT function does not modify the original arrays; it creates a new array as the result.

4. Examples

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

Example 1 - Concatenating two arrays:

SELECT ARRAY_CAT(ARRAY[1, 2, 3], ARRAY[4, 5, 6]) AS concatenated_array;

Output:

concatenated_array
------------------
[1, 2, 3, 4, 5, 6]

Example 2 - Concatenating three arrays:

SELECT ARRAY_CAT(ARRAY['apple', 'banana'], ARRAY['cherry'], ARRAY['date']) AS concatenated_array;

Output:

concatenated_array
------------------
[apple, banana, cherry, date]

Example 3 - Concatenating arrays stored in columns:

CREATE TABLE arrays (
id INT PRIMARY KEY,
array1 INT[],
array2 INT[]
);

INSERT INTO arrays VALUES (1, ARRAY[1, 2], ARRAY[3, 4]);

SELECT id, ARRAY_CAT(array1, array2) AS concatenated_array
FROM arrays;

Output:

id | concatenated_array
---+-------------------
1 | [1, 2, 3, 4]
  • array_append - Append an element to an array
  • ARRAY_PREPEND - Prepend an element to an array
  • ARRAY_CONCAT - Concatenate multiple arrays into a single array