SQL 拆分字符串

Create function [dbo].[split]

(

@SourceSql varchar(max),

@StrSeprate varchar(10)

)

returns @temp table(line varchar(max))

as

begin

    declare @i int

    set @SourceSql = rtrim(ltrim(@SourceSql))

    set @i = charindex(@StrSeprate,@SourceSql)

    while @i >= 1

    begin

        if len(left(@SourceSql,@i-1))>0

        begin

            insert @temp values(left(@SourceSql,@i-1))

        end

        set @SourceSql=substring(@SourceSql,@i+len(@StrSeprate),len(@SourceSql)-@i)

        set @i=charindex(@StrSeprate,@SourceSql)

    end

    if @SourceSql <> ''

        insert @temp values(@SourceSql)

    return

end


-----测试

GO

Select * From dbo.split('asd,f,gh,jk,l',',')
DECLARE @C NVARCHAR(MAX)
SELECT @C=OrderContent FROM dbo.Export WHERE intBobyInfoID=15538

SELECT line [content] FROM dbo.split(
(SELECT top 1 OrderContent FROM dbo.Export)
,'*')

 

posted @ 2014-01-03 11:14  JasonGu0  阅读(174)  评论(0编辑  收藏  举报