SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PARSE FULL NAME INTO LASTNAME,FIRSTNAME, MI


PARSE FULL NAME INTO LASTNAME,FIRSTNAME, MI

Author
Message
Nilssond
Nilssond
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1651 Visits: 556
I have name data to import into a table. This data is in the following
format:

DOE/JOHN F
JONES/MARY ALICE
SMITH/HAROLD

I need to parse this data into lastname, firstname, and middle
initial. Is there any TSQL I can quickly employ?



GSquared
GSquared
SSC Guru
SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)SSC Guru (118K reputation)

Group: General Forum Members
Points: 118311 Visits: 9730
You can split on the slash pretty easily, but the rest is trickier. For example, will you have any names that don't have a middle name? How about ones with multiple "middle" names ("Billy Bob Sam Jones")? How about splitting names with titles in them, like Dr or Rev? Or can you be certain all the names will be in the same format?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Lowell
Lowell
SSC Guru
SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)

Group: General Forum Members
Points: 154907 Visits: 41543
it takes a bit of manipulating, but you need to use substrings and charindex to find the slash, and grab the left and right parts of it; you have to do the same logic a second time on the "right" side of the string, and look for the space.

note my example where i added 'RUCKUS/BILLY JOE JIM BOB' what is his first name vs his middle name? you need to look at your data and see if there are any names with more than two spaces.
here's an example:

results:

LNAME FirstAndMiddle MNAME LNAME
------------------------ ------------------------ ------------------------ ------------------------
DOE JOHN F F JOHN
JONES MARY ALICE ALICE MARY
SMITH HAROLD HAROLD
RUCKUS BILLY JOE JIM BOB JOE JIM BOB BILLY




and here's the code:

SELECT 'DOE/JOHN F' AS FULLNAME INTO #TMP UNION ALL
SELECT 'JONES/MARY ALICE' UNION ALL
SELECT 'SMITH/HAROLD' UNION ALL
SELECT 'RUCKUS/BILLY JOE JIM BOB'


SELECT
--assumes the slash is ALWAYS in the data:
SUBSTRING(FULLNAME,1,CHARINDEX('/',FULLNAME) -1) AS LNAME, --minus one to remove the slash
SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30) AS FirstAndMiddle, --add one to remove the slash
CASE
--if there is a space, ASSUME the second word is a middle name, and not a two part first name
WHEN CHARINDEX(' ',SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30)) > 1
THEN SUBSTRING(SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30),CHARINDEX(' ',SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30)),30)
--else no middle name
ELSE ''
END AS MNAME,
CASE
--if there is a space, ASSUME the second word is a first name,
WHEN CHARINDEX(' ',SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30)) > 1
THEN SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,(CHARINDEX(' ',FULLNAME)-1) - CHARINDEX('/',FULLNAME)+ 1)
--else whole thing is first name
ELSE SUBSTRING(FULLNAME,CHARINDEX('/',FULLNAME)+ 1,30)
END AS LNAME
from #TMP




Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Nilssond
Nilssond
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1651 Visits: 556
Thank you for your quick and thorough replies. When I ran
the 2nd post SQL, I received the following message

Invalid length parameter passed to the SUBSTRING function.



Lowell
Lowell
SSC Guru
SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)SSC Guru (154K reputation)

Group: General Forum Members
Points: 154907 Visits: 41543
that would occur if any space existed in the last name, before the slash.
a name like Mac Bride/Bob Lee would make that happen... you'll need to tweak it based on your data.
from your example, i assumed the only space was between first and middle names.

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Nilssond
Nilssond
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1651 Visits: 556
Thank you. I'll try and tweek the SQL you prepared based on that assumption.



LutzM
LutzM
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47771 Visits: 13559
Lowell (1/13/2010)
that would occur if any space existed in the last name, before the slash.
a name like Mac Bride/Bob Lee would make that happen... you'll need to tweak it based on your data.
from your example, i assumed the only space was between first and middle names.

The lack of a slash would cause that error as well...
Seems like it's going to be quite a bit of work to get all the special patterns covered...

But that's the price one has to pay when dealing with (over-)denormalized data, I guess...



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search