SA0178 : LIKE operator is used without wildcards
Introduction
Section titled “Introduction”The use of the LIKE operator without wildcard characters can lead to unexpected query behavior.
Description
Section titled “Description”The LIKE operator is commonly used to search for a specified pattern within a column. However, using LIKE without any wildcard characters (such as % or _ ) is effectively equivalent to using the = operator for string comparison. This is prone to errors as it might not be the intended operation, leading to potential issues in query logic.
For example:
-- Example of a potentially problematic querySELECT * FROM Employees WHERE LastName LIKE 'Smith';In the above query, using LIKE without wildcards makes it functionally identical to WHERE LastName = 'Smith' . This might not be what the database administrator intends, especially if they mean to perform a broader pattern match.
-
This can lead to inefficient queries that don’t leverage full text-search capabilities when wildcards are not used.
-
It can result in future maintenance issues, as the intention behind using
LIKEmight not be clear to others examining the code.
How to fix
Section titled “How to fix”Resolve issues with the use of the LIKE operator without wildcards in SQL queries.
Follow these steps to address the issue:
1.Review the query to understand why LIKE is used without wildcards. If intended, clarify the reason in comments for future maintenance.
2.If the use of LIKE is intended for exact matches, replace it with the = operator for better performance and clarity. For example, update WHERE LastName LIKE 'Smith' to WHERE LastName = 'Smith' .
3.Consider the handling of trailing whitespace. Use = if ignoring trailing spaces, as LIKE considers every character, including trailing spaces.
4.If a pattern match is intended, include appropriate wildcards with LIKE (e.g., LIKE 'Smith%' ).
For example:
-- Corrected query using equals operator for exact matchSELECT * FROM Employees WHERE LastName = 'Smith';
-- Using LIKE operator with wildcard for pattern matchSELECT * FROM Employees WHERE LastName LIKE 'Smith%';The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| IgnoreNonDMLStatements | Parameter specifies whether to ignore LIKE predicate with pattern starting with ’%’ when it appears inside non-DML statements. | yes |
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”5 minutes per issue.
Categories
Section titled “Categories”Design Rules, Bugs
Additional Information
Section titled “Additional Information”Example Test SQL
Section titled “Example Test SQL”SELECT LocationIDFROM LocationsWHERE Specialities LIKE 'A__les'
SELECT LocationIDFROM LocationsWHERE Specialities LIKE 'App'
SELECT LocationIDFROM LocationsWHERE Specialities LIKE 'A[^p]'
SELECT LocationIDFROM LocationsWHERE Specialities LIKE 'Apples' /*IGNORE:SA0178*/
SELECT LocationIDFROM Locations /*IGNORE:SA0178(STATEMENT)*/WHERE Specialities LIKE 'pples'
IF db_name() like '%za%'PRINT db_name()Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0178 : LIKE operator is used without wildcards. | 7 | 29 |