w3resource

PostgreSQL ARRAY_TO_STRING() function

ARRAY_TO_STRING() function

TThe ARRAY_TO_STRING() function in PostgreSQL converts an array to a single string by concatenating its elements, using a specified delimiter. An optional parameter can be provided to replace null values in the array.

Uses of the PostgreSQL ARRAY_TO_STRING() Function
  • Concatenate Array Elements: Combine array elements into a single string with a specified delimiter.

  • Handle Null Values: Replace null values in the array with a specified string during concatenation.

  • Format Arrays for Display: Convert arrays to a more readable string format for display purposes.

  • Data Export: Prepare array data for export by converting it to a delimited string. Prepare array data for export by converting it to a delimited string.

Syntax:

array_to_string(anyarray, text [, text])

Return Type:

text

PostgreSQL Version: 9.3

Example: PostgreSQL ARRAY_TO_STRING() function

Code:

SELECT array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*');

Sample Output:

 array_to_string
-----------------
 1,2,3,*,5
(1 row)

Previous: ARRAY_REPLACE function
Next: ARRAY_UPPER function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/postgresql_array_to_string-function.php