Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query with NOT LIKE IN

Please help me to write a sql query with the conditions as 'NOT LIKE IN'

Select * from Table1 where EmpPU NOT Like IN ('%CSE%', '%ECE%', '%EEE%') 

Getting error.

like image 466
venkat Avatar asked Feb 22 '12 10:02

venkat


People also ask

How do I write a not like query in SQL?

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 != And <> the same in SQL?

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.

Is it better to use <> or != In SQL?

Both are valid, but '<>' is the SQL-92 standard.


2 Answers

You cannot combine like and in. The statement below would do the job though:

Select * from Table1  where EmpPU NOT Like '%CSE%'  AND EmpPU NOT Like '%ECE%'  AND EmpPU NOT Like '%EEE%' 
like image 132
Paddy Avatar answered Sep 17 '22 17:09

Paddy


That's because you're mixing two syntax together.

If you always have exactly those three values, you can just AND the results of three LIKE expressions.

SELECT   * FROM   Table1 WHERE       EmpPU NOT LIKE '%CSE%'   AND EmpPU NOT LIKE '%ECE%'   AND EmpPU NOT LIKE '%EEE%' 

If you need to do it for "any number" of values, you can put the values into a table and do a join.

WITH   myData AS (             SELECT '%CSE%' AS match   UNION ALL SELECT '%ECE%' AS match   UNION ALL SELECT '%EEE%' AS match )  SELECT   * FROM   Table1 LEFT JOIN   myData     ON Table1.EmpPU LIKE myData.match WHERE   myData.match IS NULL 

OR...

WITH   myData AS (             SELECT '%CSE%' AS match   UNION ALL SELECT '%ECE%' AS match   UNION ALL SELECT '%EEE%' AS match )  SELECT   * FROM   Table1 WHERE   NOT EXISTS (SELECT * FROM myData WHERE Table1.EmpPU LIKE match) 
like image 22
MatBailie Avatar answered Sep 18 '22 17:09

MatBailie