SQL Query to display all rows whose column doesnot start with given string

Viewed 407

I'm using Postgres SQL where I need to find cust_contact which does not start with string character J or M while the same query can be performed in Microsoft Excel using the code below:

       SELECT cust_contact
       FROM Customers
       WHERE cust_contact LIKE '[^JM]%'
       ORDER BY cust_contact;

how can we achieve the same task using PostgreSQl

4 Answers

You may do a regex like here:

SELECT cust_contact
FROM Customers
WHERE cust_contact !~ '^[JM]'
ORDER BY cust_contact;

This would return all records for which the customer contact name does not start with J or M.

You can try this command-

   SELECT cust_contact
   FROM Customers
   WHERE cust_contact NOT LIKE "J%" OR cust_contact NOT LIKE "M%"
   ORDER BY cust_contact;

try this...

SELECT cust_contact
   FROM Customers
   WHERE cust_contact NOT LIKE "J%" AND cust_contact NOT LIKE "M%"
   ORDER BY cust_contact;

Alternative WHERE clause:

...
...
WHERE cust_contact NOT LIKE ALL ("J%","M%")
...
...
Related