3

I wrote the following query:

SELECT * from TABLENAME WHERE COLUMNNAME NOT LIKE ('%%%');

Assume that the table has only one column. The problem is that the query doesn't recogonize the % inside NOTLIKE as a character, rather as a part of the syntax itself. How do I rectify this?

  • 3
    `SELECT * FROM tablename WHERE !LOCATE('%', columnname);` – Akina Dec 10 '20 at 08:12
  • I was thinking of replacing the `%` with something else then use that in the condition but I like @Akina answer better! – FanoFN Dec 10 '20 at 08:15
  • 2
    In general - never use LIKE when you need to test for definite literal. It is designed for patterns only. – Akina Dec 10 '20 at 08:16

3 Answers3

3

You have the option of escaping characters:

SELECT * from TABLENAME WHERE COLUMNNAME NOT LIKE '%\%%';

Or:

SELECT * from TABLENAME WHERE COLUMNNAME NOT LIKE '%|%%' ESCAPE '|';
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

you can use []

SELECT * from TABLENAME WHERE COLUMNNAME NOT LIKE ('%[%]%');
ali sadeghi
  • 121
  • 3
1

This is what I had in mind:

SELECT * from TABLENAME WHERE REPLACE(COLUMNNAME,'%','findthis') NOT LIKE ('%findthis%');

By replacing the symbol with something else then use it as the condition in NOT LIKE.

FanoFN
  • 6,815
  • 2
  • 13
  • 33