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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'OR' overriding 'AND' 1

Status
Not open for further replies.

DeZiner

Programmer
May 17, 2001
815
US
I'm having a logic issue with my SQL statement. I'm building a SQL string based on form data. Here's an example of the final WHERE clause:

TITLE = 'Advertising Manager' OR TITLE = 'Advertising Assoc' OR TITLE = 'Advertising VP' AND STATE LIKE '%ca%' AND FIRSTNAME LIKE '%Chuck%'

My issue is, the 'OR' statements return results while ignoring the AND results. What is the appropriate way to return the OR results, then search within them to narrow down to compensate for the AND criteria? I know this is most likely more of a SQL forumn question, however this forumn was home for me for many years! THANKS!

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Try this:
Code:
(TITLE = 'Advertising Manager' OR TITLE = 'Advertising Assoc' OR TITLE = 'Advertising VP') AND STATE LIKE '%ca%' AND FIRSTNAME LIKE '%Chuck%'

Note the use of parenthesis for the OR statements.

Similarly you can use the IN attribute, something like:
TITLE IN ('Advertising Manager','Advertising Assoc','Advertising VP')

More on IN
____________________________________
Just Imagine.
 
So Simple, yet works like a charm! Kudos.

In your opinion is performance better one way over the other?

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
If you look at the query plan of an IN() function, it translates to a series of OR statements. It's certainly more economical to code with it, though.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
Pity the amnesiac dyslexic agnostic. He stays up all night, wondering if there really is a dog.
 
Parens have first precedence, then AND and then OR.

Cheers,

Bluetone
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top