SQL Query Returning Unwanted Results [closed]

Posted on

Question :

I must be setting up the And/OR portion of my query incorrectly. In the sample dataset below, after filtering out the criteria in my Where clause only FR ni23 should be returned, however my query returns

resonance   userid<br>
Del         er12<br>
NULL        em23<br>
Samit       ra23<br>
            em23<br>
FR          ni23<br>

This is the syntax I attempted, but is not accurate, where does the problem lie, and what should I change to fix?

Declare @T1 Table (resonance varchar(100), userid varchar(10))

Insert Into @T1 (resonance, userid) Values
('Del', 'er12'), (NULL, 'em23'), ('Samit', 'ra23'), 
(NULL, 'axel'), ('', 'em23'), ('', 'red'), ('FR', 'ni23')

Select *
FROM @T1
WHERE userid LIKE '%[0-9]%'
AND (resonance <> 'DEL' OR resonance IS NULL OR resonance <> 'Samit')

Answer :

But still returns the record where resonance = ”

Select *
FROM @T1
WHERE userid LIKE '%[0-9]%'
    AND resonance not in ('DEL','Samit') 

You should define it as:

Select *
FROM @T1
WHERE userid LIKE '%[0-9]%'
    AND resonance not in ('DEL','Samit','') 

Leave a Reply

Your email address will not be published.