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%';
Answer from Paddy on Stack OverflowYou 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%';
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)
NOT LIKE with '%'
SQL LIKE ANd NOT LIKE - SQLTeam.com Forums
NOT LIKE Alternatives in WHERE clause – SQLServerCentral Forums
What is NOT LIKE operator in SQL?
Videos
So using MS SQL Server, I am wondering why this works (get cities that start and end with vowel)
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[AEIOU]%[AEIUO]'
but for finding cities that do not start and end with vowel I can't just add a NOT into the above SQL, instead I have to do
SELECT DISTINCT CITY FROM STATION WHERE CITY NOT LIKE '[AEIOU]%' AND CITY NOT LIKE '%[AEIUO]'
Use brackets to make it easier to see, and no commas (probably a typo)
WHERE (ColumnName NOT LIKE ‘string’)
AND (ColumnName NOT LIKE ‘string2’)
Guys,
I am doing a SELECT statement and need to combine several LIKE… & NOT LIKE things in my WHERE clause to wash the data.
It will let me do: WHERE ColumnName NOT LIKE ‘string’
But when I try to add several of these it gives me an error.
For ex:
WHERE ColumnName NOT LIKE ‘string’,
AND ColumnName NOT LIKE ‘string2’
What is the proper syntax to combine several LIKE & NOT LIKE parameters in the same WHERE clause of a Microsoft SQL Server SELECT statement?
Thank you!!!
From a logical perspective, these are exactly the same.
Even the presence of NULL does not affect it, because if the column is NULL then [Column] LIKE '%pattern%' evaluates to UNKNOWN and NOT UNKNOWN is still UNKNOWN and therefore fails the WHERE.
As far as actual implementation, not only are the logically the same, they actually execute the same code.
A careful inspection of the XML query plans reveals the below XML in the case of [Column] NOT LIKE
<Predicate>
<ScalarOperator ScalarString="NOT [fiddle_bf98353ecbfa4f60a8c1d988014ffc03].[dbo].[t].[Column] like '%pattern%'">
<Logical Operation="NOT">
<ScalarOperator>
<Intrinsic FunctionName="like">
<ScalarOperator>
<Identifier>
<ColumnReference Database="[fiddle_bf98353ecbfa4f60a8c1d988014ffc03]" Schema="[dbo]" Table="[t]" Column="Column"></ColumnReference>
</Identifier>
</ScalarOperator>
<ScalarOperator>
<Const ConstValue="'%pattern%'"></Const>
</ScalarOperator>
</Intrinsic>
</ScalarOperator>
</Logical>
</ScalarOperator>
</Predicate>
And this in the case of NOT [Column] LIKE
<Predicate>
<ScalarOperator ScalarString="NOT [fiddle_bf98353ecbfa4f60a8c1d988014ffc03].[dbo].[t].[Column] like '%pattern%'">
<Logical Operation="NOT">
<ScalarOperator>
<Intrinsic FunctionName="like">
<ScalarOperator>
<Identifier>
<ColumnReference Database="[fiddle_bf98353ecbfa4f60a8c1d988014ffc03]" Schema="[dbo]" Table="[t]" Column="Column"></ColumnReference>
</Identifier>
</ScalarOperator>
<ScalarOperator>
<Const ConstValue="'%pattern%'"></Const>
</ScalarOperator>
</Intrinsic>
</ScalarOperator>
</Logical>
</ScalarOperator>
</Predicate>
In other words, they are exactly the same.
I believe the best way is to Test yourself and check how it really works out in the Lab.
I am having Stackoverflow database in my Lab and I conducted the test and could see that there is no difference whatsoever whether it is logical read or CPU time or Execution plan.
Below is the Screenshot of Logical Reads and Time Stats:
Now, lets check the execution plan:
Also to add here that, I have an index on Location column however due to high number of rows as well as *(all columns) in select, optimizer preferred to go for Clustered Index Scan.
If I change the query and take only Location column in the select, Nonclustered index scan is performed however no change in the performance or execution plan.
Please be mindful of leading wildcard in the where clause as it will have to do full column scan instead of seek operation as it needs to search the complete string.