Perform regex (replace) in an SQL query

后端 未结 6 1557
闹比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:26

    Very specific to this pattern, but I have done similar to this in the past:

    REPLACE(REPLACE(columName, '<', '<'), '<', '<')

    broader example (encode characters which may be inappropriate in a TITLE attribute)

    REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
        columName
        -- Remove existing encoding:
        , '&', '&')
        , '"', '"')
        , ''', '''')
        -- Reinstate/Encode:
        , '&', '&')
        -- Encode:
        , '"', '"')
        , '''', ''')
        , ' ', '%20')
        , '<', '%3C')
        , '>', '%3E')
        , '/', '%2F')
        , '\', '%5C')
    

提交回复
热议问题