Difference between revisions of "Using Logical Operators and Parentheses"
From LongJump Support Wiki
imported>Aeric |
imported>Aeric |
||
(3 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
Expressions are evaluated in this order: | Expressions are evaluated in this order: | ||
#expressions within <tt>(parentheses)</tt> | #expressions within <tt>(parentheses)</tt> | ||
#expressions with <tt> | #expressions with Logical <tt>AND</tt> | ||
#expressions with <tt> | #expressions with Logical <tt>OR</tt> | ||
::''If the expression is not clearly annotated with parentheses, then <tt> | ::''If the expression is not clearly annotated with parentheses, then Logical <tt>AND</tt> takes precedence over Logical <tt>OR</tt>'' | ||
{{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=== | ===Examples=== | ||
:*Find all accounts where the state is either California, Nevada, or Washington | |||
::<tt><nowiki>(State = 'CA') OR (State = 'NV') OR (State = 'WA')</nowiki></tt> | |||
:*Find all accounts where the state is not California, Nevada, or Washington: | |||
::<tt><nowiki>(State != 'CA') AND (State != 'NV') AND (State != 'WA')</nowiki></tt> | |||
:*Find all accounts in the construction industry where the state is California or Nevada | |||
::<tt><nowiki>((State = 'CA') OR (State = 'NV')) AND (Industry = 'Construction')</nowiki></tt> | |||
:*Finds account in the State of California or accounts in construction in the state of Nevada | |||
::<tt><nowiki>State = 'CA' OR State = 'NV' AND Industry = 'Construction'</nowiki></tt> | |||
:*Also finds account in the State of California or accounts in construction in the state of Nevada (the <tt>AND (AND)</tt> operator implies brackets) | |||
::<tt><nowiki>State = 'CA' OR (State = 'NV' AND Industry = 'Construction')</nowiki></tt> | |||
:*Find accounts in California or Texas where the account name contains "Equipment" | |||
::<tt><nowiki>(Acct_Name contains 'Equipment') AND (state = 'CA' OR state ='TX')</nowiki></tt> | |||
:*Find accounts in California where the account name contains "Equipment", or any record in Texas | |||
::<tt><nowiki>(Acct_Name contains 'Equipment') AND (state = 'CA')) OR (state ='TX')</nowiki></tt> | |||
Latest revision as of 17:45, 13 June 2011
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:
- expressions within (parentheses)
- expressions with Logical AND
- expressions with Logical OR
- If the expression is not clearly annotated with parentheses, then Logical AND takes precedence over Logical OR
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')