Split function equivalent in T-SQL?

后端 未结 15 1881
深忆病人
深忆病人 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条回答
  •  旧时难觅i
    2020-11-21 07:55

    This blog came with a pretty good solution using XML in T-SQL.

    This is the function I came up with based on that blog (change function name and result type cast per need):

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE FUNCTION [dbo].[SplitIntoBigints]
    (@List varchar(MAX), @Splitter char)
    RETURNS TABLE 
    AS
    RETURN 
    (
        WITH SplittedXML AS(
            SELECT CAST('' + REPLACE(@List, @Splitter, '') + '' AS XML) AS Splitted
        )
        SELECT x.v.value('.', 'bigint') AS Value
        FROM SplittedXML
        CROSS APPLY Splitted.nodes('//v') x(v)
    )
    GO
    

提交回复
热议问题