targetted array element modification or delete without knowing the index?

Started by Gauthier, Daveover 16 years ago2 messagesgeneral
Jump to latest
#1Gauthier, Dave
dave.gauthier@intel.com

Is there a way to modify or delete an element of an array with knowledge only of the element's value? Maybe an array index finder would help?

For example

create table foo (name,text, arr text[]);
insert into foo (name,arr) values ('joe',ARRAY['a','b','c']);

update foo set arr[indexfinder('b')] = 'z';

This would update arr[2], because that's the element with value 'b'

Thanks !

#2Richard Broersma
richard.broersma@gmail.com
In reply to: Gauthier, Dave (#1)
Re: targetted array element modification or delete without knowing the index?

On Thu, Dec 24, 2009 at 10:01 AM, Gauthier, Dave
<dave.gauthier@intel.com> wrote:

Is there a way to modify or delete an element of an array with knowledge
only of the element’s value?  Maybe an array index finder would help?

I haven't tried it myself, but would the array "contains" operator work for you?

http://www.postgresql.org/docs/8.4/static/functions-array.html#ARRAY-OPERATORS-TABLE

--
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug