How to set array element to null value

Started by Brahmam Eswarover 7 years ago4 messages
#1Brahmam Eswar
brahmam1234@gmail.com

I'm trying to reset array element to null. but 3rd line of below snippet is
giving the compilation error.

FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;

--
Thanks & Regards,
Brahmeswara Rao J.

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Brahmam Eswar (#1)
Re: How to set array element to null value

2018-07-09 11:58 GMT+02:00 Brahmam Eswar <brahmam1234@gmail.com>:

I'm trying to reset array element to null. but 3rd line of below snippet
is giving the compilation error.

FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;

a) plpgsql doesn't support complex expressions on left side of assign
command, b) '' is not NULL in PostgreSQL

you can write your code some like

DECLARE r RECORD;
BEGIN
FOR i IN array_lower(x, 1) .. array_upper(x, 1)
LOOP
r := x[i];
IF r.reference_value = 'ABC' THEN
r.reference_value := NULL;
x[i] := r;
END IF;
END LOOP;
END;

Regards

Pavel

Show quoted text

--
Thanks & Regards,
Brahmeswara Rao J.

#3Thomas Kellerer
spam_eater@gmx.net
In reply to: Brahmam Eswar (#1)
Re: How to set array element to null value

Brahmam Eswar schrieb am 09.07.2018 um 11:58:

I'm trying to reset array element to null. but 3rd line of below snippet is giving the compilation error.

FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;

What data type is X exactly? It looks like a composite record type.

(Also: an empty string '' is not the same as NULL)

#4David Fetter
david@fetter.org
In reply to: Brahmam Eswar (#1)
Re: How to set array element to null value

On Mon, Jul 09, 2018 at 03:28:45PM +0530, Brahmam Eswar wrote:

I'm trying to reset array element to null.

You can do this in SQL as follows:

SELECT ARRAY(
SELECT CASE e WHEN 'ABC' THEN NULL ELSE e
FROM UNNEST(x) _(e)
)

This should really be going to pgsql-general because to is about how
to use PostgreSQL, not how to change PostgreSQL.

Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate