PostgreSQL array_cat() Function
The PostgreSQL array_cat() function concatenates two specified arrays into one array and returns the result array.
array_cat() Syntax
Here is the syntax of the PostgreSQL array_cat() function:
array_cat(array1, array2) -> array
Parameters
array1-
Required. An array to concatenate.
array2-
Required. Another array to concatenate.
Return value
The PostgreSQL array_cat() function returns an array, which is the concatenation of two arrays.
The two arrays to be concatenated should have the same data type, otherwise the array_cat() function will give an error message.
If one of the arguments is NULL, the function array_cat() returns another array that is not NULL.
If both arguments are NULL, the array_cat() function will return NULL.
array_cat() Examples
This example shows how to use the PostgreSQL array_cat() function to concatenate two integer arrays: {0,1,2} and {3,4,5}.
SELECT array_cat(ARRAY[0, 1, 2], ARRAY[3, 4, 5]);
array_cat
---------------
{0,1,2,3,4,5}You can also use the array_cat() function to concatenate two string arrays:
SELECT
array_cat(
ARRAY['zero', 'one', 'two'],
ARRAY['three', 'four', 'five']
);
array_cat
--------------------------------
{zero,one,two,three,four,five}If one of the arguments is NULL, the array_cat() function returns another array that is not NULL. E.g:
SELECT
array_cat(ARRAY[0, 1, 2], NULL),
array_cat(NULL, ARRAY[3, 4, 5]);
array_cat | array_cat
-----------+-----------
{0,1,2} | {3,4,5}You cannot concatenate two arrays of different data types. For example, you cannot concatenate an integer array with an string array like this:
SELECT
array_cat(
ARRAY[0, 1, 2],
ARRAY['three', 'four', 'five']
);
The array_cat() function will give an error.