I have a table which have a column that's storing the array values. I recently was asked to remove a particular value from array which I thought was a simple job but I ended up lost in complex functions and answers.
Here is the structure and the data of the table.
CREATE TABLE contacts (
id serial PRIMARY KEY,
name VARCHAR (100),
phones TEXT []
);
INSERT INTO contacts (name, phones)
VALUES
(
'John Doe',
'{"(408)-589-5846","(408)-589-5555"}'
),
(
'Lily Bush',
'{"(408)-589-5841"}'
),
(
'William Gate',
'{"(408)-589-5842","(408)-589-58423"}'
);
Here is the query which someone helped me to get the index values of the array, I was hoping I'd be able to somehow remove that value on the basis of this index but I couldn't find a way. So for instance I want to remove Phone number of John Doe
at 2nd index, how can I do that by just providing the index number in where clause or in some other way ?
select c.name,
p.phone,
p.idx
from contacts c
left join lateral unnest(phones) with ordinality as p(phone, idx) on true;