循环字符串,判断分隔符
create FUNCTION [dbo].[f_fenge](
@a varchar(max), --待分拆的字符串@b varchar(10) --数据分隔符
)RETURNS @table TABLE(col varchar(max)) --返回table
AS
BEGIN
declare @str varchar(max)
select @str='',@a=@a+@b
while CHARINDEX(@b,@a)>0
begin
if(datalength(SUBSTRING(@a,CHARINDEX(@b,@a),1))=1) --是不是英文逗号,主要区分中英文逗号begin
select @str=@str+LEFT(@a,CHARINDEX(@b,@a)-1)select @a=STUFF(@a,1,CHARINDEX(@b,@a),'')if LEN(@str)>0
INSERT @table VALUES(@str)
select @str=''
endelsebegin
select @str=@str+LEFT(@a,CHARINDEX(@b,@a))select @a=STUFF(@a,1,CHARINDEX(@b,@a),'')
end
endEND
RETURN