How to separate a column string value into multiple columns. Say I have a row with the name of "John Bob UnderWood JR" and another name of "Sally Woods Feld" Could anyone help me out with an example. I'm having issues doing substrings and CHARINDEX with names longer than two values. Below I have it working for a two name field, but not sure how to continue with more than two names.
SELECT substring(Username, 1, CHARINDEX(' ',Username)-1) FirstName,
substring(Username, CHARINDEX(' ',Username)+1, LEN(Username)) LastName
FROM tblName
I have also used:
Select parsename(Replace(name_ind, ' ', '.'), 3) as LastName,
parsename(Replace(name_ind, ' ', '.'), 2) as FirstName,
parsename(Replace(name_ind, ' ', '.'), 1) as MiddleName,
parsename(Replace(name_ind, ' ', '.'), 4) as Suffix
from UspfoWeb.dbo.tbl_pers_svcmbr_tbl_go;
Which comes close to the output I would like to see but when there is a suffix in a name it goes into the last name spot and if only has two names than their last name goes into the middle name column. I'm assuming I need to create an if statement to sort them correct?
Expected outcome:
firstName lastname middleName Suffix
John Bob Underwood Jr.
Sally Woods Feld