How to split a comma-separated value to columns

后端 未结 30 3963
刺人心
刺人心 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:46

    Use Parsename() function

    with cte as(
        select 'Aria,Karimi' as FullName
        Union
        select 'Joe,Karimi' as FullName
        Union
        select 'Bab,Karimi' as FullName
    )
    
    SELECT PARSENAME(REPLACE(FullName,',','.'),2) as Name, 
           PARSENAME(REPLACE(FullName,',','.'),1) as Family
        FROM cte
    

    Result

    Name    Family
    -----   ------
    Aria    Karimi
    Bab     Karimi
    Joe     Karimi
    

提交回复
热议问题