create function Get_Array_Item
(
@ArrStr varchar(8000),
@loc int,
@div varchar(12)
)
returns varchar(8000) as
begin
declare @arr varchar(8000)
declare @tmp varchar(8000)
declare @rslt varchar(8000)
declare @i int
declare @upArray int
declare @cnt int
declare @div_len tinyint
set @div_len = len(@div)
if(@ArrStr <> '')
begin
set @rslt = 'Error'
set @arr = @div + @ArrStr
set @tmp = @arr
set @upArray = ((len(@ArrStr) - len(replace(@tmp, @div, ''))) / @div_len) + 1
--// -1값이 들어오면 배열의 개수를 리턴한다.
if(@loc = -1)
begin
set @rslt = cast(@upArray as varchar(15))
return @rslt
end
else
begin
if(@loc > @upArray) or (@loc < 1)
begin
return @rslt
end
if(@loc > 0) and (@loc <= @upArray)
begin
set @i = 0
set @cnt = 0
while(@arr <> '')
begin
if(@cnt = @loc)
begin
if(charindex(@div, @arr, @i) <> 0)
begin
set @rslt = substring(@arr, @i, charindex(@div, @arr, @i) - @i)
end
else
begin
set @rslt = substring(@arr, @i, len(@arr) - (@i-1))
end
break
end
set @i = charindex(@div, @arr, @i) + @div_len
if(@i > 0)
begin
set @cnt = @cnt + 1
end
else
begin
break;
end
end
end
end
end
else
begin
if(@loc = -1)
begin
set @rslt = '0'
end
else
begin
set @rslt = 'Error'
end
end
return @rslt
end
--문제점
select BoardSample.dbo.Get_Array_Item('가나&&다라',-1,'&&') as test
로 하면 정상적으로 2라는 값이 검출이 된다.
하지만....
select BoardSample.dbo.Get_Array_Item('가나&&다라&&&&',-1,'&&') as test
로 하면 4라는 값이 검출된다.... ㅠ.ㅠ
댓글 영역