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



Follow us on Facebook and Twitter for latest update.