Replace with wildcard, in SQL

后端 未结 6 790
孤独总比滥情好
孤独总比滥情好 2020-12-19 01:46

I know MS T-SQL does not support regular expression, but I need similar functionality. Here\'s what I\'m trying to do:

I have a varchar table field which stores a b

6条回答
  •  甜味超标
    2020-12-19 01:52

    This works for SQL Server 2005 and up.

    create table strings (
      string varchar(1000)
    )
    
    insert into strings values( '/ID1:Category1/ID2:Category2/ID3:Category3/' )  
    insert into strings values( '/ID4:Category4/ID5:Category5/ID8:Category6/' )  
    insert into strings values( '/ID7:Category7/ID8:Category8/ID9:Category9/' )  
    go
    
    with  
    replace_with_wildcard ( restrung ) as 
    ( 
      select replace( string, '', '' ) 
      from strings
    
      union all 
    
      select  
        replace( restrung, substring( restrung, patindex( '%ID%', restrung ), 4 ), '' ) 
      from replace_with_wildcard 
      where patindex( '%ID%', restrung ) > 0 
    ) 
    
    select restrung
    from replace_with_wildcard 
    where charindex( ':', restrung ) = 0
    order by restrung
    
    drop table strings 
    

提交回复
热议问题