Split function equivalent in T-SQL?

后端 未结 15 1852
深忆病人
深忆病人 2020-11-21 07:23

I’m looking to split \'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15...\' (comma delimited) into a table or table variable.

Does anyone have a function that returns each one

15条回答
  •  囚心锁ツ
    2020-11-21 08:03

    here is the split function that u asked

    CREATE FUNCTION [dbo].[split](
              @delimited NVARCHAR(MAX),
              @delimiter NVARCHAR(100)
            ) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
            AS
            BEGIN
              DECLARE @xml XML
              SET @xml = N'' + REPLACE(@delimited,@delimiter,'') + ''
    
              INSERT INTO @t(val)
              SELECT  r.value('.','varchar(MAX)') as item
              FROM  @xml.nodes('/t') as records(r)
              RETURN
            END
    

    execute the function like this

    select * from dbo.split('1,2,3,4,5,6,7,8,9,10,11,12,13,14,15',',')
    

提交回复
热议问题