Recursive SQL to split CSV to table rows

╄→гoц情女王★ 提交于 2019-12-02 06:41:29

This one will work with empty strings

DECLARE @InputString VARCHAR(1000)
    SELECT @InputString = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,,1'

    SELECT SUBSTRING(',' + @InputString + ',', Number + 1,
    CHARINDEX(',', ',' + @InputString + ',', Number + 1) - Number -1)AS VALUE
    FROM master..spt_values
    WHERE type = 'p'
    AND Number <= LEN(',' + @InputString + ',') - 1
    AND SUBSTRING(',' + @InputString + ',', Number, 1) = ','
    GO

Also take a look at the comments here: Split string in SQL Server 2005+ CLR vs. T-SQL for some other ideas

Though it's too late now and also the OP has an accepted answer, but still it's worth mentioning to read the article on Split Function in Sql Server using Set base approach where the author has shown many ways of achieving the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!