How to use ESCAPE CLAUSE In SQL
To search for a row pattern containing a specific “escape character” we have to go for “Escape Clause“. In the following example, it uses the character (/) as the escape character and treats the % character as a literal string instead of a wildcard. Remind that without the ESCAPE clause, the query would return an empty result set.
Query
SELECT *
FROM Article
WHERE name LIKE '%\%%' ESCAPE '\';
The Result Looks Like:
Explanation
- (%) : wildcard character to match any sequence of characters.
- (\%) : the pattern , where \ is the escape character, and % is the literal %
It retrieves articles where the name column contains the percentage sign %. We can adjust the escape character and pattern based on our specific needs.
SQL Server LIKE Operator
The SQL Server LIKE operator is similar to the SQL LIKE Operator. It retrieves the rows by matching a string or character-specified pattern. A pattern can include regular characters wildcard characters or both. It is often used in the WHERE clause of the SELECT, UPDATE, and DELETE statements to filter rows based on pattern matching. It is a flexible operator that helps in finding pattern matching when you don’t know the exact pattern.
Syntax:
SELECT column1, column2
FROM table_name
WHERE col_name LIKE pattern ;
Explanation: In the above query, we have used the LIKE Operator to find the pattern in the particular column called col_name in table table_name. We will understand everything in detail in the below examples.