如标题所示,在ms sql数据库,实现一个功能,查找某一字符串在目标字符串中所在的位置。
在数据库实现这个函数:
create function [dbo].[svf_findposition] ( @target nvarchar(4000), @listsource nvarchar(4000), @delimit nvarchar(1) = n',' ) returns int as begin declare @i int,@position int,@entry nvarchar(4000) if len(isnull(@target,n'')) = 0 or len(isnull(@listsource,n'')) = 0 begin return 0 end set @position = 1 while 1 = 1 begin set @i = charindex(@delimit, @listsource) if @i = 0 if @target = @listsource break else return 0 set @entry = substring(@listsource,1,@i - 1) set @listsource = substring(@listsource,@i + 1, len(@listsource)) if @target = @entry break set @position = @position + 1 end return @position end
source code