我昨天创建了以下函数,现在我遇到了一个错误:
传递给左侧或子字符串函数的长度参数无效
你们能看看我的功能吗?我真的很感激。
Create function nowFunctionNewadd
(@fladd varchar(255))
returns @tbl table(addr varchar(100), city varchar(100),
state varchar(100), Zip varchar(5))
as
begin
declare @str varchar(100)
,@i int
,@j int
,@str2 varchar(100)
,@address varchar(100)
,@city varchar(100)
,@lastcomma int
,@lastPart varchar(100)
,@zipstart int
,@zip varchar(5) = ''
select @str=rtrim(ltrim(@fladd))
set @i = charindex(',', @str)
set @str2=rtrim(ltrim(substring(@str, @i+1, 999)))
set @j=CHARINDEX(',',@str2)
set @lastcomma = len(@str) - charindex(',', reverse(@str)+',')
set @lastPart = substring(@str, @lastcomma+2, 100)
set @address = REPLACE(rtrim(ltrim(substring(@str,1,@i-1))),',','')
set @zipstart = patindex('%[0-9]%', @lastpart)
set @city=LTRIM(RTRIM(substring(@str, @i+1, @j-1)))
If @zipstart > 0
select @zip = substring(@lastpart, @zipstart, 5),
@lastPart = rtrim(substring(@lastpart, 1, @zipstart-1))
insert into @tbl(addr, city, state, Zip)
values(@address, @city, @lastpart, @zip)
return
end