Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Latest article by SQL Server MVP Erland Sommarskog is a gemTrapping errors when working with linked servers »
    comments

    This post expands on my previous post on the similar topic
    Parsing the Address field to its individual components

    I encourage the readers who are unfamiliar with the CROSS APPLY technique used in this blog to read this very interesting blog by Brad Schulz
    T-SQL Tuesday #017: APPLY: It Slices! It Dices! It Does It All!

    This time I will be solving a similar problem of parsing names to individual components. It is based on the following MSDN thread.

    Given the names in the format of LastName (Suffix), FirstName (MiddleInitial) (where I used parenthesis to show optional parts), split the name into individual parts.

    Here is the script that does it using my favorite CROSS APPLY technique and step by step approach:

    1. /*create table #temp
    2. (
    3.     FULLNAME        VARCHAR(100),
    4.     ID              INT
    5. )
    6.  
    7. INSERT INTO #TEMP VALUES ('TUCKER, KEVIN G', 1)
    8. INSERT INTO #TEMP VALUES ('SCOTT, JOHN', 2)
    9. INSERT INTO #TEMP VALUES ('ERIC, T W', 3)
    10. INSERT INTO #TEMP VALUES ('MUNICH, SMITH D', 4)
    11. INSERT INTO #TEMP VALUES ('LYOD SR, CLIVE G', 5)
    12. INSERT INTO #TEMP VALUES ('HANSEN JR, CHARLES S', 6)
    13. INSERT INTO #TEMP VALUES ('BROWN,SHERMAN', 7)
    14. INSERT INTO #TEMP VALUES ('ANDREWS III, CLARK A', 8)
    15. INSERT INTO #TEMP VALUES ('MAMMTAN, MARY LOU', 9)
    16. */
    17. DECLARE  @Suffixes  TABLE(
    18.                           Suffix VARCHAR(5)
    19.                           )
    20.  
    21. INSERT INTO @Suffixes
    22. VALUES     ('I'),
    23.            ('II'),
    24.            ('III'),
    25.            ('IV'),
    26.            ('V'),
    27.            ('SR'),
    28.            ('JR'),
    29.            ('1st'),
    30.            ('2nd'),
    31.            ('3rd')
    32.  
    33. SELECT T.id,
    34.        T.Fullname,
    35.        F7.*,
    36.        F4.[LAST Name],
    37.        F4.Suffix
    38. FROM   #temp T
    39.        CROSS APPLY (SELECT LEFT(T.FullName,CHARINDEX(',',T.FULLNAME + ',') - 1) AS cLastName,
    40.                            LTRIM(SUBSTRING(T.FullName,CHARINDEX(',',T.FULLNAME + ',') + 1,
    41.                                            LEN(T.FullName))) AS cFirstName) F1
    42.        CROSS APPLY (SELECT LEFT(F1.cLastName,CHARINDEX(' ',F1.cLastName + ' ') - 1) AS LName,
    43.                            SUBSTRING(F1.cLastName,CHARINDEX(' ',F1.cLastName + ' ') + 1,
    44.                                      LEN(F1.cLastName)) AS pSuffix) F2
    45.        CROSS APPLY (SELECT CASE
    46.                              WHEN LEN(pSuffix) > 0
    47.                                   AND EXISTS (SELECT 1
    48.                                               FROM   @Suffixes S
    49.                                               WHERE  S.Suffix = pSuffix) THEN 'Y'
    50.                              ELSE 'N'
    51.                            END AS SuffixExists) F3
    52.        CROSS APPLY (SELECT CASE
    53.                              WHEN F3.SuffixExists = 'Y' THEN F2.LName
    54.                              ELSE RTRIM(F2.LName + ' ' + F2.pSuffix)
    55.                            END AS [LAST Name],
    56.                            CASE
    57.                              WHEN F3.SuffixExists = 'Y' THEN F2.pSuffix
    58.                              ELSE ''
    59.                            END AS [Suffix]) F4
    60.        CROSS APPLY (SELECT LEFT(F1.cFirstName,CHARINDEX(' ',F1.cFirstName + ' ') - 1) AS FName,
    61.                            SUBSTRING(F1.cFirstName,CHARINDEX(' ',F1.cFirstName + ' ') + 1,
    62.                                      LEN(F1.cFirstName)) AS MInitial) F5
    63.        CROSS APPLY (SELECT CASE
    64.                              WHEN LEN(MInitial) = 1 THEN 'Y'
    65.                              ELSE 'N'
    66.                            END AS MIExists) F6
    67.        CROSS APPLY (SELECT CASE
    68.                              WHEN F6.MIExists = 'Y' THEN F5.FName
    69.                              ELSE RTRIM(F5.FName + ' ' + F5.MInitial)
    70.                            END AS [FIRST Name],
    71.                            CASE
    72.                              WHEN F6.MIExists = 'Y' THEN F5.MInitial
    73.                              ELSE ''
    74.                            END AS [Middle Initial]) F7

    This code does not consider complex cases of 2 last names following by a suffix or first name consisting of two names following by the middle initial.

    Hopefully this short blog is useful.

    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    Naomi Nosonovsky, Senior Software Developer, has more than 15 years of enterprise experience in analysis, design, development and implementation of high-performance client/server data management solutions. She is a Personality of the Year for 2008, 2009, 2010 and 2011 at www.universalthread.com in .NET, SQL Server & VFP categories. She is also an All Star contributor/MCC Community Contributor at forums.asp.net and MSDN T-SQL forum.
    Social SitingsFacebookLinkedIndeliciousLTD RSS Feed
    1358 views
    Instapaper

    4 comments

    Comment from: chaospandion [Member] Email
    chaospandion I'm all for pushing the boundaries of any given language but there comes a time where you really should use F#. :)
    02/17/11 @ 17:03
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) Speaking from experience, splitting a full name in to individual parts is extremely difficult, and nearly impossible to get right 100% of the time.

    For example, your code does not appear to accommodate titles, like Mr., Ms., Mrs., Dr. etc...

    That being said... sometimes a 95% solution is better than no solution at all.
    02/17/11 @ 17:18
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky How would you split a name in F#?

    02/17/11 @ 17:49
    Comment from: chaospandion [Member] Email
    chaospandion @Naomi - Unfortunately there is a bit too much to explain in a comment but I may work it into a blog post.
    02/18/11 @ 09:38

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)