I have a NAME
column and the data is stored like this in a MySql database:
Doe,John P
I would like to break it out into FNAME
LNAME
MNAME
and FULLNAME
.
If I don't want to tamper with the SQL table, should I create a view?
I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server.
This is what I have so far. I thinking I need a compound query or something similar.
SELECT Name
, SUBSTRING_INDEX(Name,',',-1) As FNAME
, SUBSTRING_INDEX(Name,',',1) As LNAME
, SUBSTRING_INDEX(Name,' ',1) As MNAME
FROM people;
I'm having trouble grabbing the middle name and doing what I think should be CONCAT
function to get FULLNAME
.
It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. (i.e. DOE,JOHN).