Solved

TSQL TO PARSE NAMES

Posted on 2011-09-28
4
353 Views
Last Modified: 2012-05-12
I want to parse out FIRST, MIDDLE, and  LAST names using TSQL.

The problem I have is that data names are separated by space only.


select SUBSTRING(TBANAME, 1, CHARINDEX(' ', TBANAME) - 1) AS [Last_Name] from MYTABLE .... but not sure how to get the rest....

Here is sample of data field name [TBANAME]:


[TBANAME]
ARROYO JOEL E                
ENGLANOFF JOSEPH SHIMON      
CRAWFORD SHANNON DAVID        
FASSIHI AMIR ALI              
CASTILLO FRANCISCO ALVIN      
GUIRGUIS SYLVIA MARY E        
HATTI VIKRAM MYSORE          
KAM CHRISTINE NORRED          
0
Comment
Question by:epicazo
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36718227
There is no easy way to do this. Your data contains situations where there are 4 parts to a name.

GUIRGUIS SYLVIA MARY E

You'll also struggle with names like:

Jean-Claude Van Damme

As the last name is a two part name and you cannot determine in code which names are like this.

You could get first and last names and assume everything else is the middle. For example:


select left(tbaname, CHARINDEX(' ', tbaname) - 1) AS [First_Name],
       REPLACE(replace(tbaname, left(tbaname, CHARINDEX(' ', tbaname)), ''), reverse(left(reverse(tbaname), CHARINDEX(' ', reverse(tbaname)))), '') as [middle_name],
       reverse(left(reverse(tbaname), CHARINDEX(' ', reverse(tbaname)) - 1)) AS [Last_Name]
 from mytable

Open in new window

0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36718261
Assuming the order is always last first middle, and that a single space is always the separator:

SELECT 
    LEFT(TBANAME, CHARINDEX(' ', TBANAME + ' ') - 1) AS LName,
    RTRIM(SUBSTRING(TBANAME, CHARINDEX(' ', TBANAME + ' ') + 1, 
        CHARINDEX(' ', TBANAME + '  ', CHARINDEX(' ', TBANAME + ' ') + 1) - 
        CHARINDEX(' ', TBANAME + ' '))) AS FName,
    SUBSTRING(TBANAME, CHARINDEX(' ', TBANAME + '  ', 
        CHARINDEX(' ', TBANAME + ' ') + 1) + 1, LEN(TBANAME)) AS Mname
FROM SomeTable    

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36718272
My code above assume that, in the case of GUIRGUIS SYLVIA MARY E, that:

LName: GUIRGUIS
FName: SYLVIA
MName: MARY E

It also works with, say GUIRGUIS

LName: GUIRGUIS
FName: <zero length string>
MName: <zero length string>

GUIRGUIS SYLVIA:

LName: GUIRGUIS
FName: SYLVIA
MName: <zero length string>

If TBANAME is null, then all three name parts come back null.
0
 

Author Closing Comment

by:epicazo
ID: 36718598
Thanks so much.

The TSQL you provided worked great, but I had to modify it to extract only first letter of MIDDLE name because it works better for the data I have...

SELECT CASE WHEN LTRIM(RTRIM(SUBSTRING(TBANAME2,LEN(TBANAME2)-3,4))) IN ('MD','DO','M.D.','D.O.') then 'DR' else '' end as [nam_pfx_cd]
      ,LEFT(TBANAME, CHARINDEX(' ', TBANAME + ' ') - 1) AS [Last_Name],RTRIM(SUBSTRING(TBANAME, CHARINDEX(' ', TBANAME + ' ') + 1, CHARINDEX(' ', TBANAME + '  ', CHARINDEX(' ', TBANAME + ' ') + 1) - CHARINDEX(' ', TBANAME + ' '))) AS [First_Name]
      ,SUBSTRING(TBANAME, CHARINDEX(' ', TBANAME + '  ', CHARINDEX(' ', TBANAME + ' ') + 1) + 1, LEN(TBANAME)) AS Mname, SUBSTRING(TBANAME, CHARINDEX(' ', TBANAME + ' ', CHARINDEX(' ', TBANAME + ' ') + 1) + 1,1) as [Middle_Name]
from MYTABLE
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now