FUNCTION [dbo].[StrSplit]( @String nvarchar(MAX),--待分割字符串 @分割符 nvarchar(10)=‘,‘,--分割符 @0String int=0 --为【1】过滤空字符串 )returns @array table(item nvarchar(MAX),ID int) as begin declare @begin int,@end int,@l int,@item nvarchar(MAX) ,@ID int=0; SET @L=LEN(@分割符) set @begin = 1; set @end=charindex(@分割符,@String,@begin); while(@end<>0) begin set @item = substring(@String,@begin,@end-@begin); IF @0String<>1 or len(@item)>0 begin
SET @ID=@ID+1;
insert into @array(item,ID) values(@item,@ID);
end; set @begin = @end+@L; set @end=charindex(@分割符,@String,@begin); end set @item = substring(@String,@begin,len(@String)+1-@begin); if (len(@item)>0) begin
SET @ID=@ID+1;
insert into @array(item,ID) values(@item,@ID); end; return end