Getting first and last name

I have a field which has name of client. I need to group the firstname,middle initial into one column and the lastname into another column.

eg  Anthony N. lampoon

Anthony N. as Firstname
Lampoon as LastName
Who is Participating?
Shahan AyyubConnect With a Mentor Senior Software Engineer - iOSCommented:
Check this out:

select fullname, left(fullname,patindex('%[A-Z,a-z][ |.]%',fullname)+3)              as firstName, 
                 Right(fullname,len(fullname)-patindex('%[A-Z,a-z] %',fullname)-3) as LastName   from YOUR_TABLE

Open in new window


This script expects names like:
Anthony N. lampoon
Anthony N lampoon
What application are you trying to do this in?
what language are you using?
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

olongusAuthor Commented:
Sql in sql server 2008
Paul MacDonaldDirector, Information SystemsCommented:
This link will offer some insight.  As will this one.  Also this one.  These all rely on finding the first or last space in the column.

This will be almost impossible to do correctly, however.  Oscar de la Renta - whose first name is "Oscar" and last name is "de la Renta", will end up like "Oscar de la" and "Renta".  This gets worse if the name includes a middle name and/or a suffix (such as Jr or Sr).
Is there any particular pattern how you gonna achive this?
Assume table A(a, ...)
Field a content format as firstName I. lastname.

Use yourDatabase;
select a, LEFT(a, charindex('.',a)) AS fn,RIGHT(a,len(a)-charindex('.',a)) As ln From A;
Shahan AyyubSenior Software Engineer - iOSCommented:
you can also use this:

select left(fullname,patindex('%.%',fullname)) as firstName, Right(fullname,len(fullname)-patindex('%.%',fullname)) as LastName from YOUR_TABLE

Open in new window

assume fullname has concatenated names and always there is a "." exists in this field.
olongusAuthor Commented:
Some of the name have a "." and other do not. A few example of the names I have to contend with

Sam R. Willow III
Sam R. Willow

Results required:

Sam R.  firstname
Willow III lastname

Sam R. firstname
Willow lastname
David Johnson, CD, MVPOwnerCommented:
Too bad it wasn't set up properly in the first place 4 fields

firstname, middleinit, lastname, suffix
full_name = firstname 
if middleinit -ne NULL {  fullname = fullname + " " + middleinit
fullname = fullname + " " + lastname
if (suffix -ne NULL) {fullname = fullname + " " + Suffix}
return fullname;

Open in new window

The example you give of "Sam R. Willow III"   ( and other titles that may appear after the surname, like "Jr" ), and the previous example of "de laRenta"  demonstrates that you're probably not going to be able to automate this 100%.

So, use some of he suggestions above to get most of the way there, then check manually.

How many names ? If not enormous, you may be able to do some useful work by importing to Excel, using the Text to Columns function, and then picking out the 'diffficult cases' which will often ( but not always) be the ones that have 4 part names - so the 4th column in Excel will get a value. Identify them, then fix them manually.
It is a waste of time to have a bad data entry formats and then try to develop routines to organize it.
I suggest you manually clean the data to fit the fname i. lname.

Later, write the validation rules to acquire data in proper format.
olongusAuthor Commented:
Thank you all for your help and comments...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.