sm86
sm86

Reputation: 87

AND OR not working correctly in where clause

I am trying to exclude the following code but it keeps returning ones that shouldn't be there or excludes too many when I use 'and'. What am I doing wrong?

select idnum
,svcdat
,swpanum
,svccod
,modcod
from test1
where swpanum not in (1,16,19)
or (svccod <> 't587' or modcod not in ('u4','u5')
and svccod <> 'w447' or modcod <> 'x'
and svccod <> 'h322' or modocd <> 'kh')

Upvotes: 0

Views: 40

Answers (1)

Thom A
Thom A

Reputation: 95564

Parenthesis are important. I suspect you want:

SELECT idnum,
       svcdat,
       swpanum,
       svccod,
       modcod
FROM dbo.test1
WHERE swpanum NOT IN (1,16,19)
   OR ((svccod <> 't587' OR modcod NOT IN ('u4','u5'))
  AND  (svccod <> 'w447' OR modcod <> 'x')
  AND  (svccod <> 'h322' OR modocd <> 'kh'));

Upvotes: 1

Related Questions