Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

sql

mysql

How do I seperate the Last Name and First Name into new columns?

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

There is no delimiter when we go from left. While we go from right the delimiter is space. The last sub-string is always the Last Name. The rest is the First Names.

For Example:

"John Smith" --> "John" in column1 and "Smith" in column2

"John & Anna Smith" ---> "John & Anna" in column1 and "Smith" in column2

like image 798
Shyam Natraj Kanagasabapathy Avatar asked Dec 02 '25 06:12

Shyam Natraj Kanagasabapathy


1 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;
like image 145
Muthu Kumaran Avatar answered Dec 04 '25 20:12

Muthu Kumaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!