Is there a way to remove a value from an array in pgSQL? Or to be more precise, to pop the last value? Judging by this list the answer seems to be no. I can get the result I wan
Here is a function I use for integer[] arrays
CREATE OR REPLACE FUNCTION array_remove_item (array_in INTEGER[], item INTEGER)
RETURNS INTEGER[]
LANGUAGE SQL
AS $$
SELECT ARRAY(
SELECT DISTINCT $1[s.i] AS "foo"
FROM GENERATE_SERIES(ARRAY_LOWER($1,1), ARRAY_UPPER($1,1)) AS s(i)
WHERE $2 != $1[s.i]
ORDER BY foo
);
$$;
This is obviously for integer arrays but could be modified for ANYARRAY ANYELEMENT
=> select array_remove_item(array[1,2,3,4,5], 3);
-[ RECORD 1 ]-----+----------
array_remove_item | {1,2,4,5}