I have a table with first_name, last_name and name. Name consists of a full name such as "Joe Bloggs". I want to update every row in the table with first_name and last_name derived from the name column, but I just cant find the right combination of update with subquery to make it work.
I have tried using:
update users a set a.first_name = subquery.first_name_new
from (select username, split_part(name, ' ',1) as first_name_new from users) as subquery
where a.username = subquery.username
I can see why this isn’t working, but I just cant find the right combination.
2
Answers
update users set first_name = split_part(name, ' ',1), last_name = split_part(name, ' ',-1)
You can do it as follows :
Demo here