pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to set array element to null value
4+ messages / 4 participants
[nested] [flat]

* How to set array element to null value
@ 2018-07-09 09:58 Brahmam Eswar <brahmam1234@gmail.com>
  2018-07-09 10:26 ` Re: How to set array element to null value Pavel Stehule <pavel.stehule@gmail.com>
  2018-07-09 10:29 ` Re: How to set array element to null value Thomas Kellerer <spam_eater@gmx.net>
  2018-07-09 13:01 ` Re: How to set array element to null value David Fetter <david@fetter.org>
  0 siblings, 3 replies; 4+ messages in thread

From: Brahmam Eswar @ 2018-07-09 09:58 UTC (permalink / raw)
  To: pgsql-general; pgsql-hackers@postgresql.org

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.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to set array element to null value
  2018-07-09 09:58 How to set array element to null value Brahmam Eswar <brahmam1234@gmail.com>
@ 2018-07-09 10:26 ` Pavel Stehule <pavel.stehule@gmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2018-07-09 10:26 UTC (permalink / raw)
  To: Brahmam Eswar <brahmam1234@gmail.com>; +Cc: pgsql-general; PostgreSQL Hackers <pgsql-hackers@postgresql.org>

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




> --
> Thanks & Regards,
> Brahmeswara Rao J.
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to set array element to null value
  2018-07-09 09:58 How to set array element to null value Brahmam Eswar <brahmam1234@gmail.com>
@ 2018-07-09 10:29 ` Thomas Kellerer <spam_eater@gmx.net>
  2 siblings, 0 replies; 4+ messages in thread

From: Thomas Kellerer @ 2018-07-09 10:29 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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)





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to set array element to null value
  2018-07-09 09:58 How to set array element to null value Brahmam Eswar <brahmam1234@gmail.com>
@ 2018-07-09 13:01 ` David Fetter <david@fetter.org>
  2 siblings, 0 replies; 4+ messages in thread

From: David Fetter @ 2018-07-09 13:01 UTC (permalink / raw)
  To: Brahmam Eswar <brahmam1234@gmail.com>; +Cc: pgsql-general

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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-07-09 13:01 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-09 09:58 How to set array element to null value Brahmam Eswar <brahmam1234@gmail.com>
2018-07-09 10:26 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-07-09 10:29 ` Thomas Kellerer <spam_eater@gmx.net>
2018-07-09 13:01 ` David Fetter <david@fetter.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox