ALTER function [dbo].[StrToList_Test](@Str varchar(max), @fg NVARCHAR(200)) returns @table table(value nvarchar(max) ) as begindeclare @tempStr nvarchar(max),@len INT = LEN(@fg); --去除前后分割符 while substring(@Str,1,@len)=@fg beginset @Str=substring(@Str,@len+1,len(@Str)) end while RIGHT(@Str,@len)=@fg beginset @Str=substring(@Str,1,len(@Str)-@len) endif(len(@Str)>0) beginwhile(charindex(@fg,@Str)>0)beginset @tempStr=substring(@Str,1,charindex(@fg,@Str)-1)insert into @table(value) values(@tempStr)set @Str=substring(@Str,charindex(@fg,@Str)+@len,len(@Str))endinsert into @table(value) values(@Str) --没有分割符保存值 end return end
调用如:select * from [dbo].[StrToList_Test]('ab||cd||ef||ghi||jg','||')