I have a table with a name field, holding a first name and last name seperated by a space.
Here’s the simple test data I’m working with:
DROP TABLE IF EXISTS students;
CREATE TABLE students (
name text,
major text,
gpa float
);
INSERT INTO students
VALUES ('John Smith', 'CS', 3.7),
('Sara Li', 'History', 3.4),
('Mike Adams', NULL, 2.6);
My goal is to update the table so that I can use the split_part function in Postgresql to place the first name and last name into a new field fname and lname respectively, using an update statement, and delimited by the space in the name.
However, I am struggling getting even one of the statements to run.
UPDATE students SET first_name = split_part(name, ' ');
I am struggling getting the query to do this for each row in the table, and I also am unsure how to approach the last name, since I need to get the last name which is after the space.
I have played with subqueries and still no success, please let me know if you can help!
try