array_replace()

A function for replacing elements in an array

array_replace() is a system function for replacing elements in an array matching a specified value with another value.

array_replace() was added in PostgreSQL 9.3.

Usage

array_replace ( anyarray, anyelement, anyelement ) → anyarray

NULL. can be specied as value for replacement, or the replacment value.

Change history

Examples

Basic execution example for array_replace():

postgres=# SELECT array_replace(ARRAY['foo','bar','baz','bar'], 'bar', 'baz');
   array_replace
-------------------
 {foo,baz,baz,baz}
(1 row)

NULL values can be used as replaced / replacement values:

postgres=# SELECT array_replace(ARRAY['foo','bar','baz',NULL], NULL, 'boo');
   array_replace
-------------------
 {foo,bar,baz,boo}
(1 row)

Categories

Arrays, System function

See also

array_remove()