How to split a comma-separated value to columns

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

    This worked for me

    CREATE FUNCTION [dbo].[SplitString](
        @delimited NVARCHAR(MAX),
        @delimiter NVARCHAR(100)
    ) RETURNS @t TABLE ( val NVARCHAR(MAX))
    AS
    BEGIN
        DECLARE @xml XML
        SET @xml = N'' + REPLACE(@delimited,@delimiter,'') + ''
        INSERT INTO @t(val)
        SELECT  r.value('.','varchar(MAX)') as item
        FROM  @xml.nodes('/t') as records(r)
        RETURN
    END
    

提交回复
热议问题