SQL: parse the first, middle and last name from a fullname field

后端 未结 23 1467
粉色の甜心
粉色の甜心 2020-11-27 10:47

How do I parse the first, middle, and last name out of a fullname field with SQL?

I need to try to match up on names that are not a direct match on full name. I\'d

相关标签:
23条回答
  • 2020-11-27 11:35

    It's difficult to answer without knowing how the "full name" is formatted.

    It could be "Last Name, First Name Middle Name" or "First Name Middle Name Last Name", etc.

    Basically you'll have to use the SUBSTRING function

    SUBSTRING ( expression , start , length )
    

    And probably the CHARINDEX function

    CHARINDEX (substr, expression)
    

    To figure out the start and length for each part you want to extract.

    So let's say the format is "First Name Last Name" you could (untested.. but should be close) :

    SELECT 
    SUBSTRING(fullname, 1, CHARINDEX(' ', fullname) - 1) AS FirstName, 
    SUBSTRING(fullname, CHARINDEX(' ', fullname) + 1, len(fullname)) AS LastName
    FROM YourTable
    
    0 讨论(0)
  • 2020-11-27 11:36

    As everyone else says, you can't from a simple programmatic way.

    Consider these examples:

    • President "George Herbert Walker Bush" (First Middle Middle Last)

    • Presidential assassin "John Wilkes Booth" (First Middle Last)

    • Guitarist "Eddie Van Halen" (First Last Last)

    • And his mom probably calls him Edward Lodewijk Van Halen (First Middle Last Last)

    • Famed castaway "Mary Ann Summers" (First First Last)

    • New Mexico GOP chairman "Fernando C de Baca" (First Last Last Last)

    0 讨论(0)
  • 2020-11-27 11:38

    Unless you have very, very well-behaved data, this is a non-trivial challenge. A naive approach would be to tokenize on whitespace and assume that a three-token result is [first, middle, last] and a two-token result is [first, last], but you're going to have to deal with multi-word surnames (e.g. "Van Buren") and multiple middle names.

    0 讨论(0)
  • 2020-11-27 11:40

    Based on @hajili's contribution (which is a creative use of the parsename function, intended to parse the name of an object that is period-separated), I modified it so it can handle cases where the data doesn't containt a middle name or when the name is "John and Jane Doe". It's not 100% perfect but it's compact and might do the trick depending on the business case.

    SELECT NAME,
    CASE WHEN parsename(replace(NAME, ' ', '.'), 4) IS NOT NULL THEN 
       parsename(replace(NAME, ' ', '.'), 4) ELSE
        CASE WHEN parsename(replace(NAME, ' ', '.'), 3) IS NOT NULL THEN 
        parsename(replace(NAME, ' ', '.'), 3) ELSE
       parsename(replace(NAME, ' ', '.'), 2) end END as FirstName
       ,
    CASE WHEN parsename(replace(NAME, ' ', '.'), 3) IS NOT NULL THEN 
       parsename(replace(NAME, ' ', '.'), 2) ELSE NULL END as MiddleName,
       parsename(replace(NAME, ' ', '.'), 1) as LastName
    from  {@YourTableName}
    
    0 讨论(0)
  • 2020-11-27 11:41

    Employee table has column "Name" and we had to split it into First, Middle and Last Name. This query will handle to keep middle name as null if name column has value of two words like 'James Thomas'.

    UPDATE Employees
    SET [First Name] = CASE 
            WHEN (len(name) - len(Replace(name, '.', ''))) = 2
                THEN PARSENAME(Name, 3)
            WHEN (len(name) - len(Replace(name, '.', ''))) = 1
                THEN PARSENAME(Name, 2)
            ELSE PARSENAME(Name, 1)
            END
        ,[Middle Name] = CASE 
            WHEN (len(name) - len(Replace(name, '.', ''))) = 2
                THEN PARSENAME(Name, 2)
            ELSE NULL
            END
        ,[Last Name] = CASE 
            WHEN (len(name) - len(Replace(name, '.', ''))) = 2
                THEN PARSENAME(Name, 1)
            WHEN (len(name) - len(Replace(name, '.', ''))) = 1
                THEN PARSENAME(Name, 1)
            ELSE NULL
            END GO
    
    UPDATE Employee
    SET [Name] = Replace([Name], '.', ' ') GO
    
    0 讨论(0)
  • 2020-11-27 11:43

    Alternative simple way is to use parsename :

    select full_name,
       parsename(replace(full_name, ' ', '.'), 3) as FirstName,
       parsename(replace(full_name, ' ', '.'), 2) as MiddleName,
       parsename(replace(full_name, ' ', '.'), 1) as LastName 
    from YourTableName
    

    source

    0 讨论(0)
提交回复
热议问题