How Do I Split a Delimited String in SQL Server Without Creating a Function?

前端 未结 11 1718
慢半拍i
慢半拍i 2020-11-29 08:28

I\'m working with a SQL Server database. I have a column which contains a delimited list, and I need to write a query which splits the values of the list into rows. From bro

11条回答
  •  执笔经年
    2020-11-29 09:16

    example using the built in master..spt_values table

    DECLARE @String VARCHAR(1000)
        SELECT @String ='1,4,77,88,4546,234,2,3,54,87,9,6,4,36,6,9,9,6,4,4,68,9,0,5'
    
        SELECT SUBSTRING(',' + @String + ',', Number + 1,
        CHARINDEX(',', ',' + @String + ',', Number + 1) - Number -1)AS VALUE
        FROM master..spt_values
        WHERE Type = 'P'
        AND Number <= LEN(',' + @String + ',') - 1
        AND SUBSTRING(',' + @String + ',', Number, 1) = ','
        GO
    

    See here for more: Split A String By Using A Number Table

提交回复
热议问题