[Last Call] Learn how to a build a cloud-first strategyRegister Now


Getting first and last name

Posted on 2012-08-16
Medium Priority
Last Modified: 2012-08-29
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
Question by:olongus

Expert Comment

ID: 38300671
What application are you trying to do this in?
LVL 12

Expert Comment

ID: 38300678
what language are you using?

Author Comment

ID: 38300701
Sql in sql server 2008
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

LVL 34

Expert Comment

by:Paul MacDonald
ID: 38301802
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).
LVL 25

Expert Comment

ID: 38301923
Is there any particular pattern how you gonna achive this?
LVL 31

Expert Comment

ID: 38302236
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;
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 38302368
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.

Author Comment

ID: 38303169
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
LVL 19

Accepted Solution

Shahan Ayyub earned 2000 total points
ID: 38303571
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
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 38303622
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

LVL 17

Expert Comment

ID: 38303843
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.
LVL 31

Expert Comment

ID: 38303847
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.

Author Comment

ID: 38304721
Thank you all for your help and comments...

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question