Re: How to remove an item from integer array type

From: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
To: ChoonSoo Park <luispark(at)gmail(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to remove an item from integer array type
Date: 2013-02-20 16:47:19
Message-ID: CAB8KJ=gpc9U6ZKp8MXWbnWDWhAfgrcUJ_08gedj8sk_7k8Lx3A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2013/2/21 ChoonSoo Park <luispark(at)gmail(dot)com>
>
> Hello Gurus,
>
> Table A has integer[] column. I need to delete specific integer value from that column.
>
> f1 | f2
> 1 {100, 101, 102, 103}
> 2 {200, 300, 400}
>
> I want to remove 101 from f2 and also preserve the order.
>
> f1 | f2
> 1 {100, 102, 103}
> 2 {200, 300, 400}
>
> I tried the following query and it did remove the 101 but it didn't preserve the order.
> update tableA set f2 = (select array_agg(X.id) from (select unnest(f2) id except select 101 id) X) where f1 = 1;
>
> What's the best way to do this?

Assuming you want to keep the values in numeric order, add an ORDER BY:

update tableA set f2 = (select array_agg(X.id) from (select unnest(f2)
id except select 101 id ORDER BY id) X) where f1 = 1;

HTH

Ian Barwick

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message ChoonSoo Park 2013-02-20 16:51:55 Re: How to remove an item from integer array type
Previous Message ChoonSoo Park 2013-02-20 16:24:17 How to remove an item from integer array type