查找某一字符串在目标字符串中所在的位置
如标题所示,在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