PostgreSQL – ARRAY_CAT Function

The array_cat function is used to concatenate two arrays together in PostgreSQL database. It takes two array arguments and returns a new array that contains all the elements from both input arrays. The order of elements in the resulting array is determined by the order in which the input arrays are passed.

Below is the syntax of the array_cat function:

array_cat(array1, array2)

Where:

array1 is the first array.

array2 is the second array.

Here’s an example usage of the array_cat function:

SELECT array_cat('{1, 2, 3}', '{4, 5, 6}') AS concatenated_array;

The output would be:

concatenated_array 
--------------------- 
{1,2,3,4,5,6} 
(1 row)

In this example, the array_cat function concatenated the elements from the two input arrays into a single array.

You can also use the array_cat function in combination with other queries. For instance, if you have a table named orders with two array columns items1 and items2, and you want to create a combined list of items from both columns:

SELECT array_cat(items1, items2) AS combined_items 
FROM orders;

This query will retrieve a new array column combined_items that contains all the elements from both items1 and items2 arrays.

Similar Posts