I have column in PostgreSQL DB and I would like to make a Flyway migration to split the full-name into the first_name and last_name based on the first occurrence of the white-space. There are some rouge rows where there is either only one word or several of them. E.g.:
| fullname |
| ---------------- |
| Admin |
| Jon Doe |
| Vincent Van Gogh |
and I want to migrate column fullname into:
| first_name | last_name |
| -----------|---------- |
| Admin | |
| John | Doe |
| Vincent | Van Gogh |
Incorrect solution: I have tried several regular expression in order to find correct regex to split the string on first white-space occurrence. Unfortunately all were unsuccessful. Can anybody help me to find proper regex for splitting the string on the first occurrence of white-space? Or maybe there is better way using other PostgreSQL method than regexp_split_to_array()?
UPDATE users
SET first_name = (regexp_split_to_array(users.full_name, '\s+'))[1],
last_name = (regexp_split_to_array(users.full_name, '\s+'))[2],
In case of '\s+' regex, array is created with 3 elements and to last_name is padded only the 2nd element in case of Vincent Van Gogh.
| first_name | last_name |
| -----------|---------- |
| Admin | |
| John | Doe |
| Vincent | Van | <- Missing Gogh surname