SQL Split function that handles string with delimeter appearing between text qualifiers?

后端 未结 3 789
庸人自扰
庸人自扰 2021-01-25 08:09

There are several SQL split functions, from loop driven, to using xml commands, and even using a numbers table. I haven\'t found one that supports text qualifiers.

Usin

3条回答
  •  执念已碎
    2021-01-25 09:05

    This is a quick solution, and it is less than perfect, it has no stack, so it will treat the comma inside the quotes as the delimiter.

    alter function fnSplit
    (
        @Delim char(1),
        @List nvarchar(4000)
    )
    returns table as
    return
        with 
        Strings(PosIdx) as 
        (
            select 1 
            union all 
            select PosIdx + 1 from Strings where PosIdx < 4000
        )
        select
            ltrim(rtrim(substring(@List, PosIdx, charindex(@Delim, @List + @Delim, PosIdx) - PosIdx))) as value
        from   
            Strings
        where  
            PosIdx <= convert(int, len(@List))
        and substring(@Delim + @List, PosIdx, 1) = @Delim 
    go
    select * from fnSplit(',', 'jsmith@anywhere.com, "Sally \"Heat\" Jones" , "Mark Jones" , "Stone, Ron" ') 
        option (maxrecursion 0)
    

提交回复
热议问题