SELECT from 3rd comma in string

后端 未结 4 614
难免孤独
难免孤独 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 05:49

    try this:

    Do a substring from 3rd comma to the end of the string.. To find the 3 commas , I am using charindex() function 3 times

      declare @str varchar(50)='bzip2,1,668,some,text'
    
      select substring(@str,
      CHARINDEX(',',@str,CHARINDEX(',',@str,CHARINDEX(',',@str,1)+1)+1)+1,
      LEN(@str)-CHARINDEX(',',@str,CHARINDEX(',',@str,CHARINDEX(',',@str,1)+1)+1))
    

    result:

    some,text
    

提交回复
热议问题