獲取字符串指定字符的第n次出現位置


create function uf_findx (@text nvarchar(max),@find_x varchar(200),@find_n int)
returns int
as
begin
--第n位無效參數返回0
if @find_n<1
return (0);
--字符串不含指定字符串返回0
else if CHARINDEX(@find_x,@text)=0
return (0);
else
begin

declare @index int =1,@count_nd int=1,@len int=1
--循環獲取第n個指定字符串位置
while @find_n>=@count_nd
begin

select @index=CHARINDEX(@find_x,@text,@len)
set @count_nd=@count_nd+1
set @len=@index+1
--第n個指定字符串超過最大個數直接返回0
if @index=0
return (0);

end
end
return (@index);
end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM