w3resource

PostgreSQL ARRAY_REPLACE() function

ARRAY_REPLACE() function

The ARRAY_REPLACE() function in PostgreSQL is used to substitute each occurrence of a specified value within an array with a new value. This function is useful for modifying arrays by updating specific elements without altering the rest of the array.

Uses of the PostgreSQL ARRAY_REPLACE() Function
  • Update Specific Elements: Replace specific values in an array with new values.

  • Data Correction: Correct erroneous values in arrays.

  • Dynamic Array Modification: Adjust arrays by substituting particular elements dynamically.

  • Data Transformation: Transform arrays by changing specified elements to meet new criteria.

Syntax:

array_replace(anyarray, anyelement, anyelement)

Return Type:

anyarray

PostgreSQL Version: 9.3

Example: PostgreSQL ARRAY_REPLACE() function

Code:

SELECT array_replace(ARRAY[1,2,5,4], 5, 3);

Sample Output:

 array_replace
---------------
 {1,2,3,4}
(1 row)

Previous: ARRAY_REMOVE function
Next: ARRAY_TO_STRING function



Follow us on Facebook and Twitter for latest update.