SELECT from 3rd comma in string

后端 未结 4 701
难免孤独
难免孤独 2021-01-14 05:27

I have the following string:

bzip2,1,668,sometext,foo,bar

How can I SELECT only sometext,foo,bar? The length of the string pre

4条回答
  •  渐次进展
    2021-01-14 06:11

    Code:

    declare @input varchar(max) = 'bzip2,1,668,s,o,m,e,t,e,x,t,f,o,o,b,a,r'
    --declare @input varchar(max) = 'bzip2,,'
    declare @thirdCommaPosition int = nullif(charindex(',', @input, nullif(charindex(',', @input, nullif(charindex(',', @input, 1),0)+1),0)+1 ),0)
    select stuff(@input, 1, @thirdCommaPosition, '')
    

    Output:

    s,o,m,e,t,e,x,t,f,o,o,b,a,r
    

    Edit

    Added nullif's to the third comma calculation part, without them it's possible to get inconsistent results.

提交回复
热议问题