Search code examples
sqlsql-servert-sqlsql-like

How to write the LIKE syntax where the search term includes a percentage (%)?


I am using SQL Server 2012 and I need to perform a search on a specific field, called Notes. The search criteria is to find all rows where the term 8% is mentioned in that specific field.

The WHERE clause of my T-SQL query looks like this:

WHERE [Notes] like '%[8%]%'

However, the query is not filtering correctly based on the above syntax. It is also including rows where the term 8 is mentioned.

I had a look at the answers proposed in the question below, but they are still not giving me the correct answer.

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


Solution

  • A single character class represents a single character. So [%] means a literal percent symbol, and 8[%] means literal 8%. Try this:

    SELECT * FROM yourTable WHERE [Notes] like '%8[%]%'
    

    Demo