Solved

TSQL TO PARSE NAMES

Posted on 2011-09-28
4
386 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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