SQL Query with NOT LIKE IN

SqlSql Server-2008Tsql

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

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%'

Solution 2 - Sql

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)

Solution 3 - Sql

If you have set of words which you want to include/exclude in search from a particular column. You may want to use regular expression function of mysql.

Exclude set of words from a column :

SELECT
  *
FROM
  Table1
WHERE
      EmpPU NOT REGEXP 'CSE|ECE|EEE';

Search set of words from a column :

SELECT
  *
FROM
  Table1
WHERE
      EmpPU REGEXP 'CSE|ECE|EEE';

Solution 4 - Sql

you cant combine LIKE and IN

you can do:

select * from Table1
where EmpPU not in ('%CSE%', '%ECE%', '%EEE%')

but you wont benefit from the % wildcard

if you need the % the only option is:

Select * from Table1
where EmpPU not like '%CSE%' and  EmpPU not like '%ECE%' and EmpPU not like '%EEE%'

Solution 5 - Sql

Or you can do it like this:

SELECT 
	* 
FROM 
	Table1
WHERE NOT EXISTS
	(
		SELECT
			NULL
		FROM
		(
			SELECT '%CSE%' AS column1 UNION ALL 
			SELECT '%ECE%' UNION ALL 
			SELECT '%EEE%'
		) AS tbl
		WHERE Table1.EmpPU LIKE tbl.column1
	)

Solution 6 - Sql

you can try this

Select * from Table1 where 
    EmpPU NOT Like '%CSE%'  
AND EmpPU NOT Like '%ECE%' 
AND EmpPU NOT Like '%EEE%'

Solution 7 - Sql

Code is as below:

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

Solution 8 - Sql

Or use EXCEPT:

    SELECT * FROM Table1 
    EXCEPT
    SELECT * FROM Table1 
    WHERE EmpPU LIKE '%CSE%'  
       OR EmpPU LIKE '%ECE%' 
       OR EmpPU LIKE '%EEE%'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvenkatView Question on Stackoverflow
Solution 1 - SqlPaddyView Answer on Stackoverflow
Solution 2 - SqlMatBailieView Answer on Stackoverflow
Solution 3 - SqlshashankqvView Answer on Stackoverflow
Solution 4 - SqlDiegoView Answer on Stackoverflow
Solution 5 - SqlArionView Answer on Stackoverflow
Solution 6 - SqlZykuView Answer on Stackoverflow
Solution 7 - SqlAlbatrosView Answer on Stackoverflow
Solution 8 - SqlBruno Manuel Rosas MarquesView Answer on Stackoverflow