Split function equivalent in T-SQL?

后端 未结 15 2187
深忆病人
深忆病人 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 07:57

    CREATE Function [dbo].[CsvToInt] ( @Array varchar(4000)) 
    returns @IntTable table 
    (IntValue int)
    AS
    begin
    declare @separator char(1)
    set @separator = ','
    declare @separator_position int 
    declare @array_value varchar(4000) 
    
    set @array = @array + ','
    
    while patindex('%,%' , @array) <> 0 
    begin
    
    select @separator_position = patindex('%,%' , @array)
    select @array_value = left(@array, @separator_position - 1)
    
    Insert @IntTable
    Values (Cast(@array_value as int))
    select @array = stuff(@array, 1, @separator_position, '')
    end
    

提交回复
热议问题