Solved

TSQL TO PARSE NAMES

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 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

18 Experts available now in Live!

Get 1:1 Help Now