SQL 'LIKE' query using '%' where the search criteria contains '%'

Learn sql 'like' query using '%' where the search criteria contains '%' with practical examples, diagrams, and best practices. Covers sql, sql-server, sql-like development techniques with visual ex...

Escaping the '%' Wildcard in SQL LIKE Queries

Hero image for SQL 'LIKE' query using '%' where the search criteria contains '%'

Learn how to correctly use the SQL LIKE operator when your search string itself contains the '%' wildcard character, preventing unexpected results and ensuring accurate data retrieval.

The SQL LIKE operator is a powerful tool for pattern matching in database queries. It uses wildcard characters, most notably the percent sign (%) to match any sequence of zero or more characters, and the underscore (_) to match any single character. However, a common challenge arises when the actual data you're searching for contains a wildcard character, particularly the percent sign. If not handled correctly, the LIKE operator will interpret this literal percent sign as a wildcard, leading to unintended search results.

Understanding the Problem

Consider a scenario where you have a product catalog, and some product names or descriptions legitimately include the percent sign, perhaps indicating a discount or a chemical composition (e.g., 'Product 10% Off'). If you try to search for 'Product 10% Off' using a standard LIKE query, the database will interpret the % as a wildcard, potentially matching 'Product 100 Off', 'Product 10X Off', or any other string that starts with 'Product 10' and ends with ' Off'. This can lead to an overly broad and incorrect result set.

flowchart TD
    A[Start Query] --> B{Search String Contains '%' ?}
    B -->|No| C[Use Standard LIKE]
    B -->|Yes| D[Escape '%' Character]
    C --> E[Execute Query]
    D --> E
    E --> F[Return Results]
    F --> G[End Query]

Decision flow for handling '%' in SQL LIKE queries

The Solution: Using the ESCAPE Clause

SQL provides the ESCAPE clause specifically for this purpose. It allows you to define a character that will be used to escape the wildcard characters (% and _) within your LIKE pattern. When the database encounters the escape character immediately followed by a wildcard character, it treats the wildcard character as a literal character rather than a pattern-matching symbol.

Practical Examples

Let's illustrate this with a simple example. Suppose we have a table Products with a column ProductName.

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(255)
);

INSERT INTO Products (ProductID, ProductName) VALUES
(1, 'Product A 10% Discount'),
(2, 'Product B 20% Off'),
(3, 'Product C 50 Percent Sale'),
(4, 'Product D with % symbol');

Sample Products table creation and data insertion.

Now, let's try to find products that explicitly contain '10%'.

Incorrect Query (without ESCAPE):

SELECT ProductName
FROM Products
WHERE ProductName LIKE '%10% Discount%';

This query will incorrectly match 'Product A 10% Discount' and potentially others like 'Product A 100 Discount' if they existed, because the second '%' is treated as a wildcard.

This query would match 'Product A 10% Discount' but also any product name like 'Product A 100 Discount' if it existed, because the second % is interpreted as a wildcard. To correctly search for the literal 10%, we use the ESCAPE clause:

SELECT ProductName
FROM Products
WHERE ProductName LIKE '%10\% Discount%' ESCAPE '\';
-- Expected Result: 'Product A 10% Discount'

Correctly searching for a literal '%' using the ESCAPE clause with a backslash.

In this corrected query, \% tells the LIKE operator to treat the percent sign as a literal character, not a wildcard. The ESCAPE '\' part explicitly defines \ as the escape character. You can use any character as an escape character, as long as it's not a wildcard itself and doesn't appear literally in your search string (unless you want to escape it too!).

Database-Specific Considerations

While the ESCAPE clause is standard SQL, some database systems might offer alternative or additional ways to handle this. For example, SQL Server also supports [] for character sets, which can sometimes be used to escape single characters, though ESCAPE is the more direct and portable solution for wildcards.

-- SQL Server specific example using square brackets (less common for '%' escaping)
SELECT ProductName
FROM Products
WHERE ProductName LIKE '%10[%] Discount%';
-- This works for single characters but ESCAPE is preferred for wildcards.

SQL Server specific example using square brackets for escaping, though ESCAPE is more robust.

Always refer to your specific database system's documentation for any nuances or preferred methods, but the ESCAPE clause remains the most universally applicable and recommended approach.