I'm using Postgresql 8.0.2 Redshift
I have a bunch of names in a database that look like this stored under the value full_name:
full_name
Jeff Davis
Michael Scott
Eric Wilson
Anna Jones-Porter
Lisa Marie Scott
A.J. Jackson
What I'm trying to do is create two new fields for first_name and last_name.
I initially tried the following query:
SELECT
full_name,
REGEXP_SUBSTR(full_name, '^\\w+') first_name,
REGEXP_SUBSTR(full_name, ' \\w+') last_name
FROM
schema.table
But that only handles the first three records because it only accounts for people who have two character strings in their full_name separated by one space. So it doesn't work for the bottom three records.
What would it look like if I wanted to write a query that properly captures ALL these variations of first and last names? For ones like Lisa Marie Scott the first_name would be Lisa and last_name would be Scott.
Expected results would look like:
full_name first_name last_name
Jeff Davis Jeff Davis
Michael Scott Michael Scott
Eric Wilson Eric Wilson
Anna Jones-Porter Anna Jones-Porter
Lisa Marie Scott Lisa Scott
A.J. Jackson A.J. Jackson