我编写了函数来获取LastName,Credentials,Suffix和Middle Name。我用名字取代所有这些的输出来获得名字。我得到FirstName作为FirstName的null insted。任何人都可以帮我解决它。
alter function FirstName(@Name varchar(100))
returns varchar(25)
as
begin
declare @FirstName varchar(70)
declare @LastName varchar(100)
declare @Suffix varchar(100)
declare @Cred1 varchar(100)
declare @Cred2 varchar(100)
declare @MiddleName varchar(100)
set @LastName = dbo.LastName(@Name)
set @Suffix = dbo.Suffix(@Name)
set @Cred1 = dbo.Cred(@Name)
set @Cred2 = dbo.Cred2(@Name)
set @MiddleName = dbo.MiddleName(@Name)
set @FirstName = @Name
set @FirstName =case when len(@Cred1) >0
then stuff(@FirstName,charindex('%'+@Cred1+'%',@Name),len(@Cred1),' ')
else @FirstName
end
set @FirstName =case when len(@Cred2) >0
then stuff(@Name,charindex('%'+@Cred2+'%',@Name),len(@Cred2),' ')
else @FirstName
end
set @FirstName =case when len(@Suffix) >0
then stuff(@Name,charindex('%'+@Suffix+'%',@Name),len(@Suffix),' ')
else @FirstName
end
set @FirstName =case when len(@LastName) >0
then stuff(@FirstName,charindex('%'+@LastName+'%',@Name),len(@LastName),' ')
else @FirstName
end
set @FirstName =case when len(@MiddleName) >0
then stuff(@Name,charindex('%'+@MiddleName+'%',@Name),len(@MiddleName),' ')
else @FirstName
end
return @FirstName
end
答案 0 :(得分:0)
如果charindex('%'+@MiddleName+'%',@Name)
返回负值或长于其尝试操作的字段长度的值,例如@FirstName
或@Name
,然后返回空字符串。
我会检查您正在使用的值,看看是否是这种情况。
查看http://msdn.microsoft.com/en-us/library/ms188043.aspx以查看此功能的详细信息。