2 qq 20497589 qq_20497589 于 2016.03.07 10:25 提问

使用sql anywhere创建自定义函数 5C

使用sql anywhere创建自定义函数:GetSegStr(源串,分隔串,序号),返回字符串
功能:获取指定序号的按分隔串分隔的源串,
举例:
GetSegStr('abc-def-gh', '-', 1) = abc
GetSegStr('abc-def-gh', '-', 2) = def
GetSegStr('abc-def-gh', '-', 3) = gh

下面是SQL代码 帮忙改成SQL anywhere 谢谢

create function GetSegStr ( @source varchar(1000),@div varchar(10),@char_idx int )
returns varchar(1000)
as
begin
declare @c varchar(1000)
declare @i int
declare @i_p int
declare @s int
declare @e int

set @i = 0
set @e = 0
set @s = 0

while @i < @char_idx and charindex(@div,@source,@s + 1) > 0
begin
set @s = @e
set @e = charindex(@div,@source,@s + 1)
set @i = @i + 1
end
if @e = 0
set @e = LEN(@source) + 1
return substring(@source,@s + 1,@e - @s - 1 )
end

1个回答

devmiao
devmiao   Ds   Rxr 2016.03.08 12:52
Csdn user default icon
上传中...
上传图片
插入图片