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

后端 未结 4 1438
执笔经年
执笔经年 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:44

    One method uses recursive CTEs:

    with cte as (
          select id,
                 left(stuff(myString, 1, charindex('(', myString), ''),
                      charindex(')', myString) - charindex('(', myString) - 1
                     ) as val,
                 stuff(myString, 1, charindex(')', myString) + 1) as rest
          from t
          where myString like '%(%)%'
          union all
          select id,
                 left(stuff(rest, 1, charindex('(', rest), ''),
                      charindex(')', rest) - charindex('(', rest) - 1
                     ) as val,
                 stuff(rest, 1, charindex(')', myString) + 1) as rest
          from cte
          where rest like '%(%)%'
         )
    select id, val
    from cte;
    

提交回复
热议问题