Get string between 2 characters that repeats several times in sql server

后端 未结 4 1442
执笔经年
执笔经年 2021-01-21 22:58

I need to extract multiple strings between 2 specific characters that repeats several times in a row. for example; these are 2 rows from my table:

id myString
         


        
4条回答
  •  终归单人心
    2021-01-21 23:34

    Answer works in sqlserver 2016

    DECLARE @t table(id int, myString varchar(40))
    INSERT @t 
    VALUES (1,'aaa(bb)ccc(ffffd)'),(2, 'eeee(ff)gggg(hhh)iii')
    
    SELECT id, stuff(value, 1, charindex('(',value),'') myString
    FROM 
      @t t
    CROSS APPLY
      STRING_SPLIT(mystring,')') 
    WHERE value like '%(%'
    ORDER BY id
    

    Fiddle

提交回复
热议问题