How can I escape square brackets in a LIKE clause?

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

    The ESCAPE keyword is used if you need to search for special characters like % and _, which are normally wild cards. If you specify ESCAPE, SQL will search literally for the characters % and _.

    Here's a good article with some more examples

    SELECT columns FROM table WHERE 
        column LIKE '%[[]SQL Server Driver]%' 
    
    -- or 
    
    SELECT columns FROM table WHERE 
        column LIKE '%\[SQL Server Driver]%' ESCAPE '\'
    

提交回复
热议问题