Perform regex (replace) in an SQL query

后端 未结 6 1535
闹比i
闹比i 2020-12-01 11:08

What is the best way to replace all \'<\' with < in a given database column? Basically perform s/<[^;]/</gi

Notes

6条回答
  •  抹茶落季
    2020-12-01 11:43

    I think this can be done much cleaner if you use different STUFF :)

    create table test
    (
        id int identity(1, 1) not null,
        val varchar(25) not null
    )
    
    insert into test values ('< <- ok, < <- nok')
    
    WHILE 1 = 1
    BEGIN
        UPDATE test SET
            val = STUFF( val , PATINDEX('%<[^;]%', val) + 3 , 0 , ';' )
        FROM test
        WHERE val LIKE '%<[^;]%'
    
        IF @@ROWCOUNT = 0 BREAK
    END
    
    select * from test
    

提交回复
热议问题