PostgreSQL -ARRAY_REPLACE Function
The array_re
place function in PostgreSQL is used to replace all occurrences of a specified value from an array. It takes three arguments: the array from which you want to replace elements, the value that you want to replace and the value you want to replace with.
Below is the syntax of the array_replace
function:
array_replace(array, old_value, new_value)
Where:
array
is the array from which you want to remove elements.
is the value you want to replace from the array.old_value
is the value you want to replace with.new_value
Here’s an example of using the array_replace
function:
SELECT array_replace('{10, 20, 30, 40, 20}', 20, 50) AS result_array;
The output would be:
result_array
--------------
{10,50,30,40,50}
(1 row)
In this example, the array_replace
function replaced all occurrences of the value 20
with 50 in the input array.