How can I escape square brackets in a LIKE clause?

后端 未结 10 2113
被撕碎了的回忆
被撕碎了的回忆 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:05

    If you would need to escape special characters like '_' (underscore), as it was in my case, and you are not willing/not able to define an ESCAPE clause, you may wish to enclose the special character with square brackets '[' and ']'.

    This explains the meaning of the "weird" string '[[]' - it just embraces the '[' character with square brackets, effectively escaping it.

    My use case was to specify the name of a stored procedure with underscores in it as a filter criteria for the Profiler. So I've put string '%name[_]of[_]a[_]stored[_]procedure%' in a TextData LIKE field and it gave me trace results I wanted to achieve.

    Here is a good example from the documentation: LIKE (Transact-SQL) - Using Wildcard Characters As Literals

提交回复
热议问题