How can I escape square brackets in a LIKE clause?

后端 未结 10 1909
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:28

I am trying to filter items with a stored procedure using like. The column is a varchar(15). The items I am trying to filter have square brackets in the name.

For ex

10条回答
  •  情话喂你
    2020-11-22 15:02

    According to documentation:

    You can use the wildcard pattern matching characters as literal characters. To use a wildcard character as a literal character, enclose the wildcard character in brackets.

    You need to escape these three characters %_[:

    '5%'      LIKE '5[%]'      -- true
    '5$'      LIKE '5[%]'      -- false
    'foo_bar' LIKE 'foo[_]bar' -- true
    'foo$bar' LIKE 'foo[_]bar' -- false
    'foo[bar' LIKE 'foo[[]bar' -- true
    'foo]bar' LIKE 'foo]bar'   -- true
    

提交回复
热议问题