I am currently successfully using a CASE expression to update an empty column based on attributes from other columns. For example
UPDATE table SET cat = CASE
WHEN term = '{"Boulder"}' then 'Boulder'
However, I need to do the same but on an text array and particularly when an element is in a specific position within that array.
For example if the data looks like
{"Boulder, Tree, Bush"}
WHEN position('Tree' in term) > 0 then 'Boulder'
But I receive an error message
function pg_catalog.position(character varying[], unknown) does not exist
I have used position in a function before so I am not sure why PostgreSQL does not like it in this situation.
Is there a way to using a CASE expression whilst determining the position of a text element within an array.
Apparently your
termcolumn is defined as an array, e.g.varchar[]. Thepositionfunction only works with scalar values, not with arrays.If you want to test if an element is contained in an array you need to use a different operator:
@>The expression
'{"Boulder"}'creates an array with a single element. It’s equivalent toarray['Boulder'](which I find more readable). So the abovewherecondition updates all rows where the arraytermcontains all elements of the array on the right hand side of the operator. In this case it’s only a single element you are testing for.More details about the array functions and operators can be found in the manual: http://www.postgresql.org/docs/current/static/functions-array.html
Edit after the requirements have changed
To find and update only those where boulder is in the first, second or third place, you can use this: