Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting wrong results with multiple like statement

Status
Not open for further replies.

sap1958

Technical User
Oct 22, 2009
138
US
select JobscoID,jobstitle,jobsdiv
from Jobs
where Jobstatus in ('active','retired')
group by JobscoID,jobstitle,jobsdiv
having JobscoID = 0119
and jobstitle like '%Chair%'
or jobstitle like '%Director%'

I am ok with the fist like statement. When I insert the second like statement I get records that do not belong to JobscoID 0119. What I would really like to do is put the like statement in an array like this like ('%Chair%','%Director%'). Sql 2005 does not allow me to do this. Any ideas
 
Sure you will get the wrong result.
When you have mixed AND and OR clauses use brackets to handle the logic.
Code:
select JobscoID,
       jobstitle,
       jobsdiv
from Jobs
where Jobstatus in ('active','retired')
group by JobscoID,jobstitle,jobsdiv
having JobscoID = 0119
   and (jobstitle like '%Chair%'
    or jobstitle like '%Director%')

BTW you do not need GROUP and HAVING here.
Code:
SELECT DISTINCT JobscoID,
       jobstitle,
       jobsdiv
FROM Jobs
WHERE Jobstatus in ('active','retired')
  AND JobscoID = 0119
  AND (jobstitle like '%Chair%'
   OR jobstitle like '%Director%')


Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top