PostgreSQL array_to_string() Function
The PostgreSQL array_to_string() function concatenates all elements in an array with a delimiter and returns the result.
array_to_string() Syntax
Here is the syntax of the PostgreSQL array_to_string() function:
array_to_string(array, delimiter[, null_string]) -> text
Parameters
array-
Required. array.
delimiter-
Required. delimiter.
null_string-
Optional. The string to replace the
NULLentries .
Return value
The PostgreSQL array_to_string() function returns a string that is the result of concatenating all elements in an array using a delimiter.
The array_to_string() function will return NULL if the specified array is NULL.
array_to_string() Examples
This example shows how to use the PostgreSQL array_to_string() function to convert {1, NULL, 2, 1} to a comma-separated string.
SELECT array_to_string(ARRAY[1, NULL, 2, 1], ',');
array_to_string
-----------------
1,2,1Note that the NULL element in the array is ignored. If you want to output the NULL element as a specific value, for example 0, use the following statement:
SELECT array_to_string(ARRAY[1, NULL, 2, 1], ',', '0');
array_to_string
-----------------
1,0,2,1Now, we see that in the returned string, the NULL elements output 0.