错误:在名称解析脚本中传递给右侧函数的无效长度参数

问题描述 投票:1回答:4

我有问题。当我运行以下代码时,我收到一条错误消息:

传递给右侧函数的无效长度参数。这是什么意思,它怎么会消失?它解析了一个名字像史密斯,史蒂夫史蒂夫史密斯在列名字,中间名和姓氏

UPDATE table5
SET lastName = LEFT(Name, CHARINDEX(', ', Name) - 1), 
    firstname = SUBSTRING(Name, 
                          CHARINDEX(', ', Name) + 2, 
                          CASE 
                            WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) + 1 
                            ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2)
                          END - CHARINDEX(', ', Name) - 2
                         ),
    middlename= RIGHT(Name, 
                      LEN(Name) - CASE 
                                    WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) 
                                    ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) 
                                 END
                    ) 

“name”列是varchar(50),firstname,middlename和lastname列也设置为varchar(50)。我有点难过..先谢谢你

sql sql-server tsql
4个回答
1
投票

错误是因为CHARINDEX()返回0并且SUBSTRING()需要大于0的数字。您的假设是没有中间名的正确字符串会给您错误。

你可以查看这个小提琴,让我知道这是你用于名称的格式。因为Gordon Linoff查询似乎适用于select

SQL FIDDLE

注意:

我更新了SqlFiddle

WHERE CHARINDEX(', ', Name) > 0

我的猜测是你的一些名字没有qazxsw poi

你可以验证做

,

您还可以使用您的数据检查您的其他CHARINDEX条件,以查看哪一个RETURN 0


2
投票

我更喜欢写一个函数,这是代码。

SELECT Name
FROM test
WHERE CHARINDEX(', ', Name) = 0

使用几种方案测试功能。我想我已经覆盖了大部分,但它的价值还有第二眼。我强烈建议在更新之前进行选择,看看数据是否准确或是否符合预期。

IF object_id(N'udf_NameSplitter', N'FN') IS NOT NULL
    DROP FUNCTION udf_NameSplitter
GO

CREATE FUNCTION udf_NameSplitter
(    
      @FullName VARCHAR(50),
      @NameSplitCharacter CHAR(1),
      @NamePart CHAR(50) /*FN-firstname,MN-middlName,LN-lastName*/
)
RETURNS VARCHAR(50)
AS
BEGIN

      DECLARE @StartIndex INT, @EndIndex INT, @NameTblString VARCHAR(50)

      DECLARE @NameTbl TABLE (ID INT Identity(1,1),Item NVARCHAR(1000))

      SET @StartIndex = 1
      IF SUBSTRING(@FullName, LEN(@FullName) - 1, LEN(@FullName)) <> @NameSplitCharacter
      BEGIN
            SET @FullName = @FullName + @NameSplitCharacter
      END

      WHILE CHARINDEX(@NameSplitCharacter, @FullName) > 0
      BEGIN
            SET @EndIndex = CHARINDEX(@NameSplitCharacter, @FullName)

            INSERT INTO @NameTbl(Item)
            SELECT SUBSTRING(@FullName, @StartIndex, @EndIndex - 1)

            SET @FullName = SUBSTRING(@FullName, @EndIndex + 1, LEN(@FullName))
      END

      SELECT @NameTblString = LTRIM(RTRIM(Item))
      FROM @NameTbl 
      WHERE ID = CASE WHEN @NamePart = 'LN' THEN 1 WHEN @NamePart = 'FN' THEN 2 ELSE 3 END

      RETURN (@NameTblString)
END
GO

用你的桌子

DECLARE @Name VARCHAR(50) = 'lastName   , firstname ,middleName '

SELECT 
lastName=dbo.udf_NameSplitter(@Name,',','LN'),
firstname=dbo.udf_NameSplitter(@Name,',','FN'),
middleName=dbo.udf_NameSplitter(@Name,',','MN')

1
投票

这意味着UPDATE table5 SET lastName=LEFT(Name, CHARINDEX(', ', Name) - 1), firstname=SUBSTRING(Name, CHARINDEX(', ', Name) + 2, CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) + 1 ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END - CHARINDEX(', ', Name) - 2), middlename=RIGHT(Name, LEN(Name) - CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END) 返回0,您将传递给另一个函数。解决此问题的一种方法是:

charindex()

这确保了UPDATE table5 SET lastName=LEFT(Name, CHARINDEX(', ', Name) - 1), firstname=SUBSTRING(Name, CHARINDEX(', ', Name) + 2, CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) + 1 ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END - CHARINDEX(', ', Name) - 2), middlename=RIGHT(Name, LEN(Name) - CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END) WHERE Name LIKE '% % %'; 有两个空间,这是你的逻辑假设的。


-1
投票
Name

在sql中

© www.soinside.com 2019 - 2024. All rights reserved.