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:

\ is the escape character

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.

Similar Reads

Wildcard Characters

These Wildcard characters help us to fetch data from a database or table by matching the pattern. We will understand each Wildcard character with examples....

Using NOT LIKE Operator

NOT LIKE Operator is working opposite as LIKE Operator do. The NOT LIKE Operator to find information that does not match any condition. Let’s understand with examples....

Using ESCAPE CLAUSE

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....

COLLATE CLAUSE

LIKE Operator is case-insensitive by default. To change it a case-sensitive we have to use COLLATE. To change a row as case-sensitive LIKE operator was also used....

Conclusion

The LIKE Operator in SQL Server is very helpful operator to find pattern when we don’t have exact information regarding particular pattern. With the help of this Operator one can easily find what they want using the Wildcard . It is helpful when we handle large set of data....

Contact Us