I have a data like below.
id col1[]
--- ------
1 {1,2,3}
2 {3,4,5}
My question is how to use replace function in arrays.
select array_replace(col1, 1, 100) where id = 1;
but it gives an error like:
function array_replace(integer[], integer, integer) does not exist
can anyone suggest how to use it?
Your statement (augmented with the missing
FROM
clause):As commented by @mu,
array_replace()
was introduced with pg 9.3. I see 3 options for older versions:1.
intarray
As long as ...
A simple and fast option would be to install the additional module
intarray
, which (among other things) provides operators to subtract and add elements (or whole arrays) from/to integer arrays:2. Emulate with SQL functions
A (slower) drop-in replacement for
array_replace()
using polymorphic types, so it works for any base type:Does not replace NULL values. Related:
If you need to guarantee order of elements:
3. Apply patch to source and recompile
Get the patch "Add array_remove() and array_replace() functions" from the git repo, apply it to the source of your version and recompile. May or may not apply cleanly. The older your version the worse are your chances. I have not tried that, I would rather upgrade to the current version.