Parsing Last,First Middle Name

  • Hi,

    I need to parse last name,first name space middle name.

    Example A - DOE,JOHN

    Example B - DOE,JOHN A

    I am able to parse the last name.

    left(dbo.AbstractData.Name, charindex(',', dbo.AbstractData.Name)-1) as last name

    I can parse the first and middle names together.

    ltrim(right( dbo.AbstractData.Name,(len(dbo.AbstractData.Name)-charindex(',',dbo.AbstractData.Name))))

    as firstmiddlename

    I can parse the middle name.

    SUBSTRING(dbo.AbstractData.Name,CHARINDEX(' ',dbo.AbstractData.Name + ' ')+1,LEN(dbo.AbstractData.Name))

    I am having trouble parsing the first name. The first name is everything after the comma and before the first blank after the comma (or the end of the string, in which case there is no middle name).

    Can anyone help me parse the first name only?

    Thanks

  • Not 100% certain what you are after here but take a look at this. This will parse everything into last, first, middle.

    if OBJECT_ID('tempdb..#Something') is not null

    drop table #Something

    create table #Something

    (

    SomeID int identity,

    FullName varchar(25)

    )

    insert #Something

    select 'DOE,JOHN' union all

    select 'DOE,JOHN A'

    select *

    from #Something

    cross apply dbo.DelimitedSplit8K(replace(FullName, ' ', ','), ',') p

    You can find the code for the DelimitedSplit8K function by following the link in my signature about splitting strings.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • If you ever cross with a spanish last name with spaces, you might have problems with the 8K splitter.

    This formula might have its own problems, but it's another option for you.

    if OBJECT_ID('tempdb..#Something') is not null

    drop table #Something

    create table #Something

    (

    SomeID int identity,

    FullName varchar(25)

    )

    insert #Something

    select 'DOE,JOHN' union all

    select 'DOE,JOHN A' union all

    select 'DE LA VEGA,ELENA'

    SELECT SUBSTRING( FullName, CHARINDEX(',', FullName) + 1, ISNULL(NULLIF(CHARINDEX(' ', FullName, CHARINDEX(',', FullName)), 0), 8000) - (CHARINDEX(',', FullName) + 1))

    ,CHARINDEX(',', FullName) + 1

    ,ISNULL(NULLIF(CHARINDEX(' ', FullName, CHARINDEX(',', FullName)), 0), 8000) - (CHARINDEX(',', FullName) + 1)

    FROM #Something

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • This isn't precisely what you're trying to do, but I found it helpful when trying to do some similar parsing. There's a lot you have to look out for.

    http://www.informit.com/articles/article.aspx?p=25049

  • Take a look at the procedure in this post:

    http://www.sqlservercentral.com/Forums/FindPost1472808.aspx

     

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply