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

后端 未结 23 1678
粉色の甜心
粉色の甜心 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
    

提交回复
热议问题