How to use not like operator in PostgreSQL?

by hal.littel , in category: PHP Databases , 8 months ago

How to use not like operator in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 3 months ago

@hal.littel 

In PostgreSQL, you can use the NOT LIKE operator to find rows that do not match a specific pattern.


Here is the basic syntax for using NOT LIKE in a SELECT statement:

1
2
3
SELECT column1, column2, ...
FROM table_name
WHERE column1 NOT LIKE pattern;


For example, suppose you have a table called customers with a name column. To select all rows from the customers table where the name does not contain the string 'John', you can use the following query:

1
SELECT * FROM customers WHERE name NOT LIKE '%John%';


You can also use the NOT LIKE operator with the ESCAPE clause to specify an escape character for the pattern. For example:

1
SELECT * FROM customers WHERE name NOT LIKE '%John%%' ESCAPE '';


This will select all rows where the name column does not contain the string 'John%'. The character is used as the escape character, so it tells PostgreSQL to treat the % character as a literal character and not as a wildcard.


I hope this helps! Let me know if you have any questions.