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',',')

 

posted on 2013-10-25 10:47  苏上话  阅读(389)  评论(0编辑  收藏  举报