Custom Search
Logiclabz
  • Home
  • Sql Server
  • Sql server: Special Characters (%,[,]) in LIKE operator using ESCAPE keyword

Sql server: Special Characters (%,[,]) in LIKE operator using ESCAPE keyword

  

Finding special characters such as percentage symbol on sql column would tricky. The below sql would not work to search '20%' on samplecolumn

SELECT samplecolumn FROM sampletable WHERE 
    samplecolumn LIKE '%20%%' 

Instead

SELECT samplecolumn FROM sampletable WHERE 
    samplecolumn LIKE '%20\%%' ESCAPE '\'

"ESCAPE" keyword are used within an SQL statement to tell the engine that the escaped part of the SQL string should be handled differently. In above SQL ESCAPE character is mentioned as '\' hence character after '\' is processed differently instead of normal there '%' character is search in "samplecolumn" column of "sampletable" table.

Likewise, underscore and any other special can be escaped from normal operation.

SELECT samplecolumn FROM sampletable WHERE 
    samplecolumn LIKE '%|_%%' ESCAPE '|'

In above sql '|' symbol is used as escape character.



  


Leave a reply


Comments

  • peter says:
    Sep 08, 09

    and how about it with a backslasch

  • JayPrakashSharma says:
    Dec 24, 11

    Very informative article. Its really helped me lot. Thanks for sharing with us. Check out this link too its also having a nice post related to this post with wonderful explanation... http://mindstick.com/Articles/1216a153-610b-41f1-b51b-3035c069ffd7/?%E2%80%98SELECT%E2%80%99%20command%20with%20SQL%20operator Thanks



Do you like this post?