能够从全名解析名字和姓氏,我该如何解析中间名?

时间:2017-03-01 17:41:08

标签: tsql

能够从全名解析名字和姓氏,我该如何解析中间名?没有使用的标题,例如' MR',' MS'' DR'' FR'' MRS&#39 ,' LRD' SIR',' LORD'' LADY'' MISS'' ;所以我认为我可以使用子字符串。名称格式可以是firstname lastname,或firstname lastname,中间的空格。

UPDATE p
SET    p.LAST_NAME = c.LASTNAME --tested that join is correct, contact name is combined, will need to parse it out ***, need to reference inserted

       --Need FIRST_NAME, MIDDLE_NAME, LAST_NAME
       p.FIRST_NAME = SUBSTRING(c.CONTACT, 1, CHARINDEX(' ', c.CONTACT) - 1) AS FirstName,
       p.MIDDLE_NAME = --need middle name
       p.LAST_NAME = SUBSTRING(CONTACT, CHARINDEX(' ', CONTACT) + 1, len(CONTACT)) AS LastName

FROM   GMUnitTest.dbo.CONTACT1 c 
       JOIN PCUnitTest.dbo.PEOPLE p
         ON p.PEOPLE_ID = c.KEY4  
         WHERE c.Key1 = '31';  

1 个答案:

答案 0 :(得分:0)

根据您的说法,必须是中间名,您可以使用以下内容:

declare @table table (fullName varchar(256))
insert into @table values
('First Middle Last'),
('John Mary-Lou Smith'),
('Frank NMN Sanatra')

select
    CHARINDEX(' ',fullName,1)
    ,left(fullName,CHARINDEX(' ',fullName,1) - 1) as FirstName
    ,substring(fullName,CHARINDEX(' ',fullName,1) + 1,(len(fullName) - CHARINDEX(' ',fullName,1)) - charindex(' ',reverse(fullName),1)) as MiddleName
    ,right(fullName,charindex(' ',reverse(fullName),1)) as LastName
from
    @table