Split function by comma in SQL Server 2008

后端 未结 6 917
-上瘾入骨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条回答
  •  -上瘾入骨i
    2020-12-06 22:53

    create the below function and use as below

    CREATE FUNCTION [dbo].[Split]    
     (    
      @List nvarchar(2000),    
      @SplitOn nvarchar(5)    
     )      
     RETURNS @RtnValue table     
     (    
    
      Id int identity(1,1),    
      Value nvarchar(100)    
     )     
     AS      
     BEGIN     
      While (Charindex(@SplitOn,@List)>0)    
      Begin    
    
       Insert Into @RtnValue (value)    
       Select     
        Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))    
    
       Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))    
      End    
    
      Insert Into @RtnValue (Value)    
      Select Value = ltrim(rtrim(@List))    
    
      Return    
     END  
    
    SELECT TOP 1 * FROM dbo.Split(order,',')
    

提交回复
热议问题