Escaping charachters in a LIKE clause in SQL

SQL server

To escape a characther in the LIKE clause of SQL, use the additional ESCAPE clause which is part of the SQL standard and should work with any relational DB.


WHERE username LIKE '%^_d' ESCAPE '^';

In the example above, the ^ is used as the escape charachter, which forces the query return all usernames that end with _d

Sepcifically in Microsoft T-SQL we can use also the character range operator: [], so the query becomes:


WHERE username LIKE '%[_]d'

Post a Comment

Previous Post Next Post