从SQL Server中的名称字符串中获取中间名和名字

时间:2014-05-01 04:39:56

标签: sql sql-server space comma

我有像

这样的场景
Kumar, Anand 
S,Anand D
D,Ravi,K

现在,我必须使用一个查询从SQL Server中的上述场景中提取,名字,中间名和姓氏。因为,要从全名获得中间名很难并且与名字相同,所以我尝试了

select 
   CASE 
      WHEN CHARINDEX(' ', REVERSE('D,Ravi,K')) = 0 
        THEN RIGHT('D,Ravi,K', CHARINDEX(',', REVERSE('D,Ravi,K')) - 1) 
      WHEN CHARINDEX(' ', REVERSE('D,Ravi,K')) > 0 
        THEN RIGHT('D,Ravi,K', CHARINDEX(' ', REVERSE('D,Ravi,K')) - 1)
   END AS 'MIDDLE NAME'

但是,我收到了错误

  

Msg 536,Level 16,State 1,Line 1
  传递给右侧函数的长度参数无效。

任何人都可以帮我解决这个问题吗?

任何其他技术都可以用来提取上述值。

10 个答案:

答案 0 :(得分:1)

您需要创建一个获取字符串并将其拆分为多个部分的函数。以下是功能概述:

CREATE FUNCTION splitNames (@name VARCHAR(500), @sep VARCHAR(1))
RETURNS @ret TABLE(idx INT IDENTITY(1,1), part VARCHAR(200))
AS
BEGIN
    WHILE(CHARINDEX(@sep, @name)>0)
    BEGIN
        INSERT INTO @ret (part)
        SELECT SUBSTRING(@name, 1, CHARINDEX(@sep, @name)-1)
        SET @name = SUBSTRING(@name, CHARINDEX(@sep, @name) + LEN(@sep), LEN(@name))
    END
    INSERT INTO @ret (part) 
    SELECT  @name
    RETURN
END

然后你可以在从其他表读取字符串时调用它或直接传递字符串,如下所示:

SELECT f.*
FROM   TABLE_NAME n
CROSS APPLY
(
    SELECT * FROM splitNames(n.name, ',')
)f

或者

SELECT * FROM splitNames('D, Ravi, Anand', ',')

-------------------
| Idx    |  part  |
-------------------
|   1    |  D     |
-------------------
|   2    |  Ravi  |
-------------------
|   3    |  Anand |
-------------------

答案 1 :(得分:1)

SELECT 'prabhat ku sahoo'                                                                                                      AS full_name,  
  SUBSTR('prabhat ku sahoo',1,instr('prabhat ku sahoo',' ',1,1))                                                                   AS first_name,
  SUBSTR('prabhat ku sahoo',instr('prabhat ku sahoo',' ',1,1),instr('prabhat ku sahoo',' ',1,2)-instr('prabhat ku sahoo',' ',1,1)) AS middle_name,
  SUBSTR('prabhat ku sahoo',instr('prabhat ku sahoo',' ',1,2))                                                                     AS last_name
FROM dual;

答案 2 :(得分:0)

我认为为每个名字创建字段是恰当的:

CREATE TABLE persons_tbl
(
persons_id INT(11),
first_name VARCHAR(100),
middle_name VARCHAR(100),
last_name VARCHAR(100)
);

然后得到它:

SELECT first_name, middle_name, last_name FROM persons_tbl

答案 3 :(得分:0)

发生错误,因为字符串中没有任何空格。

RIGHT('D,Ravi,K',CHARINDEX(' ', REVERSE('D,Ravi,K')) -1)

CHARINDEX返回BLANK或0。

无法为RIGHT函数赋予负值。

您可以使用REPLACE()将空格替换为空格,以使数据具有一定的一致性

答案 4 :(得分:0)

DECLARE @FullName    VARCHAR(50) = 'D,Ravi,K'

SELECT SUBSTRING(@FullName, 1, CHARINDEX(',', @FullName) - 1) AS [Middle Name],
       SUBSTRING(@FullName, 
           CHARINDEX(',', @FullName) +1,
           LEN(@FullName)- CHARINDEX(',',@Fullname)-2
       ) AS [First Name]

答案 5 :(得分:0)

请检查我的尝试:

declare @tbl as table(Col nvarchar(100))
insert into @tbl values
('Kumar,Anand'),
('S,Anand D'),
('D,Ravi,K'),
('Anulas')

select 
    Cola,
    FN,
    REPLACE(REPLACE(Col, ','+FN,''), LN+',','') MN,
    LN
from(
    select 
        *, 
        LEFT(Col, CHARINDEX(',', col, 1)-1) LN,
        REVERSE(LEFT(REVERSE(Col), CHARINDEX(',', REVERSE(Col), 1)-1)) FN
    from(
        select Col Cola,
        case LEN(Col)-LEN(REPLACE(Col,',','')) 
                when 0 then ',,'+Col
                when 1 then REPLACE(Col, ',',',,') 
                else Col end Col
        from @tbl
    )x
)y

答案 6 :(得分:0)

我做到了。谢谢你的帮助

    declare @name varchar(100)
    set @name='Sharma,Ravi,K'


 select CASE WHEN CHARINDEX(' ',REPLACE(@name,',',' '), CHARINDEX(' ',REPLACE(@name,',',' ')) + 1) >0 THEN
        SUBSTRING(REPLACE(@name,',',' '), CHARINDEX(' ',REPLACE(@name,',',' ')) + 1,(LEN(@name)-CHARINDEX(' ',REVERSE(REPLACE(@name,',',' '))))-CHARINDEX(' ',REPLACE(@name,',',' '))) else SUBSTRING(REPLACE(@name,',',' '), CHARINDEX(' ',REPLACE(@name,',',' ')) + 1,LEN(@name)) END AS 'FIRST NAME'
    ,CASE WHEN CHARINDEX(' ',REPLACE(@name,',',' '), CHARINDEX(' ',REPLACE(@name,',',' ')) + 1) >0 THEN SUBSTRING(REPLACE(@name,',',' '),LEN(@name)-CHARINDEX(' ',REVERSE(REPLACE(@name,',',' ')))+2,LEN(@name)) ELSE ' '
    END AS 'MIDDLE NAME'
    ,
    SUBSTRING(REPLACE(@name,',',' '), 1,CHARINDEX(' ',REPLACE(@name,',',' ')))

答案 7 :(得分:0)

这将适用于String为FirstName/MiddleName/LastName

的情况
Select

DISTINCT NAMES ,

SUBSTRING(NAMES , 1, CHARINDEX(' ', NAMES) - 1) as FirstName,

RTRIM(LTRIM(REPLACE(REPLACE(NAMES,SUBSTRING(NAMES , 1, CHARINDEX(' ', NAMES) - 1),''),REVERSE( LEFT( REVERSE(NAMES), CHARINDEX(' ', REVERSE(NAMES))-1 ) ),'')))as MiddleName,

REVERSE( LEFT( REVERSE(NAMES), CHARINDEX(' ', REVERSE(NAMES))-1 ) ) as LastName

From TABLENAME

答案 8 :(得分:-1)

以下是如何从First_Name获取Middle_NameLast_NameFull_Name(代替Full_Name放置您的属性名称)

   SELECT 
   LEFT(Full_Name,CHARINDEX(' ',Full_Name + ' ')-1)  AS FIRST_Name,
   Rtrim(substring(Full_Name,CHARINDEX(' ',Full_Name,0)+1,len(Full_Name)-CHARINDEX(' ',Full_Name,0)+1-CHARINDEX(' ', REVERSE(Full_Name)))) Middle_Name,
   Right(Full_Name,ISNULL(NULLIF(CHARINDEX(' ', REVERSE(Full_Name)) - 1,-1), LEN(Full_Name))) AS Last_Name
   FROM [Test].[dbo].[TEST]

答案 9 :(得分:-1)

我不知道 SQL 服务器,但在 MYSQL 中,我这样做是为了获取名字和中间名:

SELECT DISTINCT substring_index(name_column, ' ', 1) as First_name,
substring_index((substring_index(name_column, ' ', -2))," ",1) as Middle_name from table;