MySQL – SELECT LIKE %

[adsense id=”0514458240″ width=”468″ height=”60″]

This may sound easy to perform but as the percent ‘%’ is used as a wildcard in SQL this becomes a little trickier. Imagine we have a table that is full of numbers but some have been added incorrectly and include the percent symbol. We need to identify these ones.

The following SQL query will return all rows

SELECT * from table WHERE field LIKE '%%%';

What we need to do is to escape character (prepending a \) the percentage to indicate that it is not the wildcard but the actual character we are searching for.

SELECT * from table WHERE field LIKE '%\%%';

This will return the correct set of rows containing the % character.

Another way of performing this is using the regular expression search (RLIKE which is a synonym for regexp) as follows:

SELECT * from table WHERE field RLIKE '%';