在SQL中将字符串的一部分提取为列

时间:2016-09-07 20:38:45

标签: sql-server

我的源表中有一个名为Name的列 但是该字段具有人的全名,包括第一个,最后一个和中间名。在我的结果表中,我想将它们分成3个不同的列。注意:并非所有名称都包含中间名。

Example :

In the source table

Name : Kennedy, John F
Name : Paul, Sean

In the destination field I want

LastName :  Kennedy
FirstName : John
MiddleName : F 

LastName : Paul
FirstName : Sean
MiddleName : Null 

我设法编写了SQL Query以获取LastName,但无法弄清楚第一个和中间名。

Code for LastName :

 CASE
                WHEN CHARINDEX(',', (RTRIM([NAME]))) > 0 
                THEN    LEFT((RTRIM([NAME])),CHARINDEX(',',(RTRIM([NAME])))-1)
                ELSE    RTRIM([NAME])
 END as LastName

我使用SQL Server作为RDBMS

3 个答案:

答案 0 :(得分:0)

您也可以尝试ParseName()

Declare @String varchar(100)='Kennedy, John F'

Select LastName=ParseName(Replace(@String,' ','.'),3)
      ,FirstName=ParseName(Replace(@String,' ','.'),2)
      ,MiddleName=ParseName(Replace(@String,' ','.'),1)

返回

LastName    FirstName   MiddleName
Kennedy,    John        F

答案 1 :(得分:0)

Declare @YourTable table (ID int,FullName varchar(50))
Insert Into @YourTable values
(1,'Kennedy,  John F'),
(2,'Smith, Jame')

;with cteBase as (
    Select ID
          ,NewString = Replace(Replace(Replace(LTrim(RTrim(FullName)),' ','.'),',',''),'..','.')
          ,Pos=IIF(ParseName(Replace(Replace(RTrim(LTrim(FullName)),'  ',' '),' ','.'),3) is null,2,3)
     From @YourTable
)
Select ID
      ,LastName   = IsNull(ParseName(NewString,Pos-0),'')
      ,FirstName  = IsNull(ParseName(NewString,Pos-1),'')
      ,MiddleName = IsNull(ParseName(NewString,Pos-2),'')
 From cteBase

返回

LastName    FirstName   MiddleName
Kennedy,    John        F
Smith,      Jame        

答案 2 :(得分:0)

  --1.Create a Function
    Create FUNCTION [dbo].[fn_GetName]
    (
        @Descripcion nvarchar(max),
        @Tipo nvarchar(50)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
    DECLARE @HTML NVARCHAR(MAX)

        declare @Nombre nvarchar(max) = @Descripcion
        declare @Letra nvarchar(1)= ''
        Declare @PrimerBlanco int  = 0
        Declare @Conjunto nvarchar(50)=''
        Declare @Entrada int = 1
        Declare @FueLetra int = 0

        Declare @LastName nvarchar(500)
        Declare @FirstName nvarchar(500)
        Declare @MiddleName nvarchar(500)
        Declare @Resultado nvarchar(500)

        while 1 = 1
        begin 

            set @Letra = LEFT(@Nombre,1)
            set @Conjunto = @Conjunto +''+@Letra

            if @Letra = ' '  and @FueLetra = 1 
            begin 
                set @PrimerBlanco = @PrimerBlanco + 1

                if @PrimerBlanco = 1
                begin 

                    if @Entrada = 1
                    begin 
                        set @Entrada = @Entrada + 1
                        set @LastName = ltrim(rtrim(@Conjunto))
                        GOTO Fin
                    end 

                    if @Entrada = 2
                    begin 
                        set @Entrada = @Entrada + 1
                        set @FirstName = ltrim(rtrim(@Conjunto))
                        GOTO Fin
                    end 

                    if @Entrada = 3
                    begin 
                        set @Entrada = @Entrada + 1
                        set @MiddleName = ltrim(rtrim(@Conjunto))
                        GOTO Fin
                    end 

                    Fin:  
                    set @Conjunto = ''
                    set @FueLetra = 0
                end 

            end 
            else 
            begin 
                set @PrimerBlanco = 0
                if @Letra  = ' '
                begin 
                    set @FueLetra = 0
                end 
                else 
                begin   
                    set @FueLetra = 1
                end 
            end 

            if @Nombre = ''
            begin 
                break
            end 

            Set @Nombre = substring(@Nombre ,2,len(@Nombre))
        end 

        if @tipo = 'LN'
        begin 
            set @Resultado = @LastName
        end 

        if @tipo = 'FN'
        begin 
            set @Resultado = @FirstName
        end 

        if @tipo = 'MN'
        begin 
            set @Resultado = @MiddleName
        end 

        RETURN @Resultado ;
    END    

    --2. Get the value(No matter white space)
Declare @Table as table 
(
    Name nvarchar(max)
)

insert into @Table values ( '              Kennedy,               John                 F')
insert into @Table values ('           Paul,            Sean')

select 
[dbo].[fn_GetName](name,'LN') as LastName,
[dbo].[fn_GetName](name,'FN') as FirstName,
[dbo].[fn_GetName](name,'MN') as MiddleName
from @Table