How to separate last name and first names to two new columns in mysql?

How to separate last name and first name from first name in new columns?

I have "John and Anna Smith" in one column, I need to divide it into "John and Anna" and "Smith" in two columns. How to do it?

When we go left, there is no separator. As we walk to the right, the separator is space. The last substring is always the name Last Name. The rest are the first names.

Example:

“John Smith” → “John” in column 1 and “Smith” in column2

“John and Anna Smith” ---> “John and Anna” in column 1 and “Smith” in column2

+3
source share
2 answers

Hey you can try something like this

SELECT REPLACE('John & Anna Smith', SUBSTRING_INDEX('John & Anna Smith', ' ', -1),'') AS first_name, SUBSTRING_INDEX('John & Anna Smith', ' ', -1) AS last_name;
+1

: " " → "" 1 "" 2 " " --- > " " 1 "" 2

( ), . - config/xml/table, , / .

. , .

+1

All Articles