将全名拆分为姓氏名,中间名和后缀

时间:2018-10-29 03:37:17

标签: sql sql-server

如何将全名拆分为姓氏名中间名后缀? 例如: 1.史密斯·约翰·T·PHD 2.史密斯·约翰 3.史密斯·约翰·T 名称以空格分隔。此名称列具有这三种不同的类型。

Desired output:

1)史密斯-姓氏     约翰-名字     T-中间名     博士-后缀

2)SMITH--LASTNAME 约翰--FIRSTNAME

3)SMITH--LASTNAME 约翰-名字 T--MIDDLENAME

4 个答案:

答案 0 :(得分:0)

如果使用SQL 2016或Azure SQL或Azure SQL数据仓库,则可以使用string_split函数。 Read more on string_split

DECLARE @tmp TABLE (Name NVARCHAR(100))

INSERT @tmp
SELECT 'Smith John T PHD'

SELECT CASE 
        WHEN rn = 1
            THEN 'LastName'
        WHEN rn = 2
            THEN 'FirstName'
        WHEN rn = 3
            THEN 'Middlename'
        WHEN rn = 4
            THEN 'Suffix'
        END AS NameType
    ,NAMES.value
FROM (
    SELECT row_number() OVER (
            ORDER BY (
                    SELECT 0
                    )
            ) AS rn
        ,s.value
    FROM @tmp AS t
    CROSS APPLY STRING_SPLIT(t.Name, ' ') AS s
    ) AS NAMES

答案 1 :(得分:0)

         /*
            APPROACH:
            STEP 1: DECLARE FOUR VARIABLES TO HOLD -- LAST NAME, FIRST NAME, MIDDLE 
            NAME AND SUFFIX
            STEP 2: TRAVERSE THE STRING, AND KEEP A TRACK OF NUMBER OF SPACES YOU ARE 
            GETTING
             STEP 2.1 IF SPACE_NUM = 1, INITIALIZE THE STRINGS YOU ARE VISITED UPTO 
            NOW, INTO 'LAST NAME'
             STEP 2.2 IF SPACE_NUM = 2, INITIALIZE THE STRINGS YOU ARE VISITED UPTO 
            NOW, INTO 'FIRST NAME'
             STEP 2.3 IF SPACE_NUM = 3, INITIALIZE THE STRINGS YOU ARE VISITED UPTO 
            NOW, INTO 'MIDDLE NAME'
             STEP 3. INITIALIZE THE THE LAST STRINGS YOU VISITED INTO 'SUFFIX'

            NOTE: ASSUMING THE STRINGS ARE SAME FORMAT YOU MENTIONED.
            NOTE: FOR GETTING THE FIRST STRING A.K.A LAST NAME, WE HAVE TO PUT EXTRA- 
            SPACE IN FRONT OF STRING.
         */


         DECLARE @STRING AS VARCHAR(50) = 'Smith John T PHD'
         SET @STRING = ' '+@STRING

         DECLARE @FIRST_NAME AS VARCHAR(30) = ''
         DECLARE @LAST_NAME AS VARCHAR(30) = ''
         DECLARE @MIDDLE_NAME AS VARCHAR(30) = ''
         DECLARE @SUFFIX AS VARCHAR(30) = ''


         DECLARE @I AS INT = 1
         DECLARE @COUNT AS INT =0
         DECLARE @STR AS VARCHAR(20) =''

         WHILE @I <> LEN(@STRING)+1
         BEGIN
            IF SUBSTRING(@STRING, @I, 1) = ' '
            BEGIN
                SET @COUNT = @COUNT +1
                SET @I = @I +1
                CONTINUE
            END

            SET @STR += SUBSTRING(@STRING, @I, 1)

            IF @COUNT = 1
            BEGIN
                SET @LAST_NAME += @STR
                SET @STR = ''
            END

            IF @COUNT = 2
            BEGIN
                SET @FIRST_NAME += @STR
                SET @STR = ''
            END

            IF @COUNT = 3
            BEGIN
                SET @MIDDLE_NAME += @STR
                SET @STR = ''
            END
        SET @I = @I +1
        END

   SET @SUFFIX = @STR

    SELECT @FIRST_NAME AS "FIRST NAME", @LAST_NAME AS "LAST NAME", @MIDDLE_NAME 
     "Middle Name", @SUFFIX as "Suffix";

答案 2 :(得分:0)

如果全名始终采用指定格式,则可以执行以下操作:

select parsename(newcol, 4), parsename(newcol, 3), parsename(newcol, 2), parsename(newcol, 1)
from t cross apply
    ( values (replace(col, ' ', '.')) 
    ) tt(newcol);

答案 3 :(得分:0)

尝试一下

DECLARE @tmp TABLE (Name NVARCHAR(100))

INSERT @tmp
SELECT 'Smith John T PHD'

SELECT dtt.Name,dt2.SplitedName FROM
(
SELECT  ROW_NUMBER()OVER(ORDER BY (SELECT 1)) AS Id,
        Split.a.value('.','nvarchar(1000)') AS Name

FROM(
        SELECT CAST('<S>'+REPLACE(Name,' ','</S><S>')+'</S>' AS XML) Name
         FROM @tmp
    )dt 
CROSS APPLY Name.nodes('S') AS Split(a))dtt
INNER JOIN 
(   SELECT 1 AS id,  'lastname' AS SplitedName UNION ALL
        SELECT 2 ,  'firstname'  UNION ALL
        SELECT 3 ,  'middlename '  UNION ALL
        SELECT 4 ,  'Suffix' 
)dt2 ON dt2.ID=dtt.Id

结果

Name    SplitedName
-------------------
Smith   lastname
John    firstname
T       middlename 
PHD     Suffix