SQL分割字符串,返回临时表

create function [dbo].[f_split]
(
    @c varchar(2000),--需要分割的字符串(例如:1,2,3,4,5    我|和|你)
    @split varchar(2)--分隔符(例如 ,  |  $)
)
returns @t table(col varchar(200))--返回表
as
    begin
        while(charindex(@split,@c)<>0)
        begin
            insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
            set @c = stuff(@c,1,charindex(@split,@c),'')
        end
        insert @t(col) values (@c)
        return
    end
 select * from f_split('1,2,3,4,5',',')
原文地址:https://www.cnblogs.com/su-king/p/3387482.html