Write a SQL query to print words that do not contain '%' symbol in a table

Viewed 328

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 Answers

You have the option of escaping characters:

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

Or:

SELECT * from TABLENAME WHERE COLUMNNAME NOT LIKE '%|%%' ESCAPE '|';

you can use []

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

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.

Related