Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simple nested question

Author  Topic 

hbadministrator
Posting Yak Master

120 Posts

Posted - 2014-08-22 : 10:39:30
I have 2 tables one that contains 3 searches that don't work correctly.

Table
Equipment
fields
Model, active, DeliveryStatus, prod-type

The where statement is
Where active = 'A' and Model = 'FOTNK' OR Model = 'LPTNK'

The results actually show all the FO and LP tanks so thats good but it pulls all active 'A''s but also some 'I' as well. My thought is I should do a select with a where on Active first then a select for the FOTNK and LPTNK or the other way around. I just don't know how to write it. Any help is greatly appreciated.

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-08-22 : 11:09:37
AND has a higher order of precedence than OR so your current WHERE clause can be written as

WHERE (active = 'A' AND Model = 'FOTNK') OR Model = 'LPTNK'

I suspect you want:

WHERE active = 'A' AND (Model = 'FOTNK' OR Model = 'LPTNK')

which reduces to:

WHERE active = 'A' AND Model IN ('FOTNK', 'LPTNK')

Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 2014-08-22 : 11:33:17
bingo! Thank you
Go to Top of Page
   

- Advertisement -