Parse fullname field into FirstName and LastName

SQL Server 2012

I've inherited a database which has several tables which contain a FullName field, but no FirstName or LastName field.

I know this is problematic, and at best will be a 95% solution, but I need a SQL query which will split the FullName column which looks like:

James Lewis
Parker, Linda
Jones-Taylor, Brenda

into

LastName        FirstName
Lewis           James
Parker          Linda
Jones-Taylor    Brenda

Open in new window


I know this is going to involve  maybe a couple of Case statements, but I'm uncertain how to parse for the text to the right or left of the comma/first space.  I know it will look something like:

SELECT FullName,
CASE WHEN [FullName] LIKE '%,%' everything left of comma
          ELSE everything to the right of the first space END as LastName,
CASE WHEN [FullName] Like '%,%' everything right of the comma
          ELSE everything to the left of the first space END as FirstName
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
This code should get you most of the way there. It will not handle middle names or suffixes, though.

DECLARE @Test TABLE
(
	FullName VARCHAR(50)
)

INSERT INTO @Test ( FullName )
VALUES ('James Lewis'), ('Parker, Linda'), ('Jones-Taylor, Brenda'), ('John Q Smith')

SELECT FullName, 
	CASE WHEN FullName LIKE '%,%' THEN LEFT(FullName, CHARINDEX(',', FullName) - 1) 
		ELSE SUBSTRING(FullName, LEN(FullName) - CHARINDEX(' ', REVERSE(FullName)) + 2, CHARINDEX(' ', REVERSE(FullName))) END LastName,
	CASE WHEN FullName LIKE '%,%' THEN SUBSTRING(FullName, CHARINDEX(',', FullName) + 2, LEN(FullName) - CHARINDEX(',', FullName)) 
		ELSE LEFT(FullName, LEN(FullName) - CHARINDEX(' ', REVERSE(FullName))) END FirstName
FROM @Test

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
What about this?
SELECT FullName,
	LEFT(FullName, CHARINDEX(',',FullName)-1) AS LastName,
	RIGHT(FullName, LEN(FullName) - CHARINDEX(',', FullName)) AS FirstName

Open in new window

0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks, Shaun.

CharIndex, SubString, and Reverse were the key words I needed.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Vitor MontalvãoMSSQL Senior EngineerCommented:
CharIndex, SubString, and Reverse were the key words I needed.
I'm not sure if you really need all of those functions.
Have you even tried my suggestion?
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Yes,

and had to modify it to account for a couple of anomolies.  Still have a few, but that has to be expected with a free text data entry and no fields for prefix or suffix in the table.
CASE WHEN FullName LIKE '%,%' THEN SUBSTRING(FullName, CHARINDEX(',', FullName) + 2, LEN(FullName) - CHARINDEX(',', FullName)) 
		ELSE LEFT(FullName, CHARINDEX(' ', FullName)) END FirstName, 
CASE WHEN FullName LIKE '%,%' THEN LEFT(FullName, CHARINDEX(',', FullName) - 1) 
		ELSE SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName) - CHARINDEX(' ', FullName))  END LastName, 

Open in new window


I had to remove the Reverse commands because there were a number of instances in the data where it included a suffix, so all I was getting was the suffix, not everything to the right of the first space.
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Vitor,

Thanks for your comment, but you failed to take into account that some of the records contain a comma, so they represent (Last, First) and others don't have the comma, so those represent (First Last).

Dale
1
Vitor MontalvãoMSSQL Senior EngineerCommented:
I see. I only had the comma situations.
Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.