Using Logical Operators and Parentheses

From AgileApps Support Wiki
Revision as of 17:45, 13 June 2011 by imported>Aeric
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Logical operators combine subexpressions into a larger expression, to specify multiple conditions and complex criteria.

<subexpression1> <Logical_operator> <subexpression2>...

LOGICAL_OPERATOR

Logical operators can be used to build more complex expressions.
The logical operators are:
AND
OR
Considerations
  • Two subexpressions joined by a logical operator form a logical expression.
  • Logical expressions resolve to a Boolean value: 1/0 or TRUE/FALSE.
  • Use parentheses--()--to group expressions logically and to join multiple expressions.
  • Parentheses are used in pairs; each open parenthesis "(" requires a closing parenthesis ")".
  • For example:
((<expression1> AND <expression2>) OR (<expression3> AND <expression_4>))


Precedence

Expressions are evaluated in this order:

  1. expressions within (parentheses)
  2. expressions with Logical AND
  3. expressions with Logical OR
If the expression is not clearly annotated with parentheses, then Logical AND takes precedence over Logical OR

Thumbsup.gif

Tip: For any complex expression, always use parentheses. That way, you're sure to get what you expect, and the meaning is clear when you or anyone else reads it later.

Examples

  • Find all accounts where the state is either California, Nevada, or Washington
(State = 'CA') OR (State = 'NV') OR (State = 'WA')
  • Find all accounts where the state is not California, Nevada, or Washington:
(State != 'CA') AND (State != 'NV') AND (State != 'WA')
  • Find all accounts in the construction industry where the state is California or Nevada
((State = 'CA') OR (State = 'NV')) AND (Industry = 'Construction')
  • Finds account in the State of California or accounts in construction in the state of Nevada
State = 'CA' OR State = 'NV' AND Industry = 'Construction'
  • Also finds account in the State of California or accounts in construction in the state of Nevada (the AND (AND) operator implies brackets)
State = 'CA' OR (State = 'NV' AND Industry = 'Construction')
  • Find accounts in California or Texas where the account name contains "Equipment"
(Acct_Name contains 'Equipment') AND (state = 'CA' OR state ='TX')
  • Find accounts in California where the account name contains "Equipment", or any record in Texas
(Acct_Name contains 'Equipment') AND (state = 'CA')) OR (state ='TX')