Search Postgresql Archives

Re: How to remove an item from integer array type

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2013/2/21 ChoonSoo Park <luispark@xxxxxxxxx>
>
> 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


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux