How to split a comma-separated value to columns

后端 未结 30 3998
刺人心
刺人心 2020-11-21 04:38

I have a table like this

Value   String
-------------------
1       Cleo, Smith

I want to separate the comma delimited string into two colu

30条回答
  •  庸人自扰
    2020-11-21 04:45

    Try this:

    declare @csv varchar(100) ='aaa,bb,csda,daass';
    set @csv = @csv+',';
    
    with cte as
    (
        select SUBSTRING(@csv,1,charindex(',',@csv,1)-1) as val, SUBSTRING(@csv,charindex(',',@csv,1)+1,len(@csv)) as rem 
        UNION ALL
        select SUBSTRING(a.rem,1,charindex(',',a.rem,1)-1)as val, SUBSTRING(a.rem,charindex(',',a.rem,1)+1,len(A.rem)) 
        from cte a where LEN(a.rem)>=1
        ) select val from cte
    

提交回复
热议问题