Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NOT LIKE' in an SQL query

Why does this simple query return 'ORA-00936: missing expression' (the database is Oracle as you can tell):

SELECT * FROM transactions WHERE id NOT LIKE '1%' AND NOT LIKE '2%' 

I feel silly, but what am I doing wrong?

like image 790
Ariod Avatar asked Nov 19 '09 11:11

Ariod


People also ask

How do you write not like in SQL query?

SQL not like statement syntax will be like below. SELECT column FROM table_name WHERE column NOT LIKE pattern; UPDATE table_name SET column=value WHERE column NOT LIKE pattern; DELETE FROM table_name WHERE column NOT LIKE pattern; As an example, let's say we want the list of customer names that don't start with 'A'.

Is there a not like in SQL?

The NOT LIKE operator in SQL is used on a column which is of type varchar . Usually, it is used with % which is used to represent any string value, including the null character \0 .

Is != And <> the same in SQL?

If != and <> both are the same, which one should be used in SQL queries? Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.

Can we use not and like together in SQL?

The SQL language lets you combine NOT and LIKE to eliminate search results using the same type of logic except records are removed from a data set instead of adding them.


1 Answers

You have missed out the field name id in the second NOT LIKE. Try:

SELECT * FROM transactions WHERE id NOT LIKE '1%' AND id NOT LIKE '2%' 

The AND in the where clause joins 2 full condition expressions such as id NOT LIKE '1%' and can't be used to list multiple values that the id is 'not like'.

like image 177
mikej Avatar answered Sep 24 '22 00:09

mikej