从sql server中的名字拆分中间的首字母

时间:2016-05-27 08:56:49

标签: sql sql-server

我希望从名字中分割中间名,只有当名字有空格时,空格后也应该是1个字符,如果它有超过1个字符,我们应该将整个字符串视为名字。

例如:输入字符串是“MATE K”,那么它应该分成两部分,

Firstname = MATE& MiddleInitial = K

但是如果输入字符串是“MATE KATE”,则它不应该拆分并保持不变。

Firstname = MATE KATE

以下是我工作的示例,但它没有按预期给出输出。

有人可以帮帮我吗?

declare @name as varchar(50)
set @name ='MATE KATE'
select left(@name, CHARINDEX(' ', @name)) as FirstName,
substring(@name, CHARINDEX(' ', @name) +1, len(@name)-(CHARINDEX(' ',@name)-1)) as MiddleInitial

5 个答案:

答案 0 :(得分:2)

这不会考虑所有可能的坏字符串,但会给你一个起点。最好不要在SQL中使用CLR进行此操作。 SQL并不是真的应该做这样的事情并且非常有限:

DECLARE @t TABLE ( s VARCHAR(MAX) )
INSERT  INTO @t
VALUES  ( 'MATE K' ),
        ( 'MATE KATE' )


SELECT  SUBSTRING(s, 1,
                  CASE WHEN SUBSTRING(REVERSE(s), 2, 1) = ' '
                       THEN CHARINDEX(' ', s) - 1
                       ELSE LEN(s)
                  END) AS FirstName ,
        CASE WHEN SUBSTRING(REVERSE(s), 2, 1) = ' '
             THEN SUBSTRING(s, LEN(s), 1)
             ELSE NULL
        END AS MiddleName
FROM    @t

输出:

FirstName   MiddleName
MATE        K
MATE KATE   NULL

答案 1 :(得分:0)

希望这有帮助。

    declare @name as varchar(50)
set @name ='MATE    K'
select CASE WHEN CHARINDEX(' ',REVERSE(LTRIM(RTRIM(@name))),0)=2 THEN left(LTRIM(RTRIM(@name)), CHARINDEX(' ', LTRIM(RTRIM(@name))))   ELSE   LTRIM(RTRIM(@name)) END AS firstname,
CASE  WHEN CHARINDEX(' ',REVERSE(LTRIM(RTRIM(@name))),0)=2 THEN LTRIM(RTRIM(substring(LTRIM(RTRIM(@name)), CHARINDEX(' ', LTRIM(RTRIM(@name))) +1, len(LTRIM(RTRIM(@name)))-(CHARINDEX(' ',LTRIM(RTRIM(@name)))-1))))  ELSE ''  END  as MiddleInitial

答案 2 :(得分:0)

这应该可以完成这项工作,但可能会简化:

DECLARE @name AS VARCHAR(50)
SET @name = 'MATE K'

SELECT  CASE WHEN CHARINDEX(' ', @name) != LEN(@name) - 1 THEN @name
             ELSE LEFT(@name, CHARINDEX(' ', @name))
        END AS FirstName ,
        CASE WHEN CHARINDEX(' ', @name) = LEN(@name) - 1
             THEN SUBSTRING(@name, CHARINDEX(' ', @name) + 1, 1)
             ELSE ''
        END AS MiddleName

Output:
FirstName   MiddleName
MATE        K

SET @name = 'MATE KATE'

SELECT  CASE WHEN CHARINDEX(' ', @name) != LEN(@name) - 1 THEN @name
             ELSE LEFT(@name, CHARINDEX(' ', @name))
        END AS FirstName ,
        CASE WHEN CHARINDEX(' ', @name) = LEN(@name) - 1
             THEN SUBSTRING(@name, CHARINDEX(' ', @name) + 1, 1)
             ELSE ''
        END AS MiddleName

-- Output:
    Output:
FirstName   MiddleName
MATE KATE       

答案 3 :(得分:0)

DECLARE @t table 
(name varchar(20))
INSERT INTO @t(name) values 
('MATE K'),
('MATE KATE')

select 
CASE WHEN LEN(substring(name, CHARINDEX(' ', name) +1, len(name)-(CHARINDEX(' ',name)-1))) > 1 
THEN 
NAME 
ELSE  left(name, CHARINDEX(' ', name)) END as FirstName,
CASE WHEN LEN(substring(name, CHARINDEX(' ', name) +1, len(name)-(CHARINDEX(' ',name)-1))) = 1
THEN 
substring(name, CHARINDEX(' ', name) +1, len(name)-(CHARINDEX(' ',name)-1)) ELSE NULL END as MiddleInitial
FROM @t

答案 4 :(得分:0)

借助XML:

DECLARE @name varchar(50),
        @xml xml

SET @name ='Mate Kate'

SELECT @xml = CAST('<f><n>' + REPLACE(@name,' ','</n><n>') + '</n></f>' as xml)

;WITH cte AS (
SELECT  t.v.value('.','nvarchar(50)') as Names,
        CASE WHEN LEN(t.v.value('.','nvarchar(50)')) = 1 THEN 1 ELSE 0 END as LenName,
        row_number() over(order by t.v) as RowNumber
FROM @xml.nodes('/f/n') as t(v)
WHERE NULLIF(t.v.value('.','nvarchar(50)'),'') IS NOT NULL
)

SELECT 
    STUFF(
    (SELECT ' ' + NAMES
    FROM cte
    WHERE LenName = 0 OR (LenName = 1 and RowNumber = 1)
    FOR XML PATH('')),1,1,'') as FirstName,
    STUFF(
    (SELECT ' ' + NAMES
    FROM cte
    WHERE LenName = 1 AND RowNumber != 1
    FOR XML PATH('')),1,1,'') as MiddleInitial

适用于'Mate Kate'' Mate Kate '

FirstName   MiddleInitial
Mate Kate   NULL

适用于'Mate K''Mate K '

FirstName   MiddleInitial
Mate        K

适用于' M Kate ''M Kate'

FirstName   MiddleInitial
M Kate      NULL