How to split a comma-separated value to columns

后端 未结 30 3947
刺人心
刺人心 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 05:10

    With CROSS APPLY

    select ParsedData.* 
    from MyTable mt
    cross apply ( select str = mt.String + ',,' ) f1
    cross apply ( select p1 = charindex( ',', str ) ) ap1
    cross apply ( select p2 = charindex( ',', str, p1 + 1 ) ) ap2
    cross apply ( select Nmame = substring( str, 1, p1-1 )                   
                     , Surname = substring( str, p1+1, p2-p1-1 )
              ) ParsedData
    

提交回复
热议问题