0
id | name                   | status
---+------------------------+--------------------------------
1  | {'Tom','Jerry','Hary'} | { 'Waiting','Waiting',Waiting'}

I want to update status column based on name.

For example, I want to update status column to "Arrived" for name "Hary"

id | name                   | status
---+------------------------+--------------------------------
1  | {'Tom','Jerry','Hary'} | { 'Waiting',Waiting','Arrived'}

Note: I can achieve by using below query. But i don't want to use index positioning. Is there any alternative to achieve above result?

UPDATE table
SET status[3] = 'Arrived'
WHERE name[3]='Hary'
8
  • 3
    You should really normalize your table and store every array element in one separate record Commented Dec 10, 2019 at 15:20
  • 2
    Quote from the manual Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements Commented Dec 10, 2019 at 15:21
  • Just because you can store arrays in tables does not mean you should. This feature exists, but I have yet to see a usecase where it is not better to use a normalized joined table. Commented Dec 10, 2019 at 15:21
  • @Hogan: I think the "check list" when array may be used is simple: if you always treat the array as one single value in SQL, then it's probably OK to use them. Commented Dec 10, 2019 at 15:23
  • @a_horse_with_no_name -- if I understand your use case correctly then it fails in the same way, if you want to add or remove something from the checklist you have to change the whole thing -- which is not really how checklists are supposed to work. Also if you want to check if some item is on the checklist you can't. Basically it is not a checklist it a text field. Commented Dec 10, 2019 at 15:29

2 Answers 2

2

As commented by S-Man, you should just fix your data model and store each name/status on a separate record.

Otherwise, you can use array_position():

update table set status[ array_position(name, 'Harry') ] = 'Arrived'
where 'Harry' = any(name)
Sign up to request clarification or add additional context in comments.

Comments

0

GMB has shown you how to do it, but I think you should also see a normalized design (just in case you don't know how that would look):

Name table
-----------
id     int
name   varchar(70)

Status table
------------
id     int
status varchar(100)

namestatus table
----------------
nameid   int
statusid int

Data

name table
1 Tom
2 Jerry
3 Hary

status table
1 Waiting
2 Arrived

namestatus table
1    1
2    1
3    1

Update statement

UPDATE namestatus
set statusid = 2 
where nameid = 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.