Split function by comma in SQL Server 2008

后端 未结 6 919
-上瘾入骨i
-上瘾入骨i 2020-12-06 21:58

I know that this question has been asked many times but could not find what I needed.

I have this column \"Order\" which contains data in the following format.

6条回答
  •  执笔经年
    2020-12-06 22:46

    Declare @str as Varchar(100) = '10|20|30|40|500|55'
    Declare @delimiter As Varchar(1)='|'
    Declare @Temp as Table ( item varchar(100))
    Declare @i as int=0
    Declare @j as int=0
    Set @j = (Len(@str) - len(REPLACE(@str,@delimiter,'')))
    While @i  < = @j
    Begin
      if @i  < @j
      Begin
          Insert into @Temp 
          Values(SUBSTRING(@str,1,Charindex(@delimiter,@str,1)-1))
          set @str = right(@str,(len(@str)- Charindex(@Delominator,@str,1)))
      End
      Else
      Begin
         Insert into @Temp Values(@str)
      End
    
     Set @i = @i + 1
    End
    
    Select * from @Temp 
    

提交回复
热议问题