question

Viktoria avatar image
Viktoria asked

AND/OR operators, no parentheses

Hello, I am trying to read this where clause, wondering if I am reading it right: [START_DATE] <= EFFDAT AND END_DATE > EFFDAT OR [START_DATE] <= EXPDAT AND (EFFDAT != EXPDAT) Would you say that this means this: ([START_DATE] <= EFFDAT AND END_DATE > EFFDAT) OR ([START_DATE] <= EXPDAT AND EFFDAT != EXPDAT) OR THIS: ([START_DATE] <= EFFDAT AND END_DATE > EFFDAT) OR ([START_DATE] <= EXPDAT) AND (EFFDAT != EXPDAT) Thanks so much!
operatororparenthesesandno
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
Without parentheses the operators follow the rules of [Operator Precedence][1], which puts the AND before the OR, so will read ( [START_DATE] <= EFFDAT AND END_DATE > EFFDAT ) OR ( [START_DATE] <= EXPDAT AND (EFFDAT != EXPDAT) ) However as you have seen it is better for human readability to always include the parentheses [1]: http://msdn.microsoft.com/en-us/library/ms190276.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Oleg avatar image
Oleg answered
The first one reflects the meaning because logical **AND** has a precedence over **OR**. That said, it is just plain evel not to segregate the statements with parentheses in order to make the intentions clear. Here is the BOL link: [ http://msdn.microsoft.com/en-us/library/ms186992.aspx][1] Oleg [1]: http://msdn.microsoft.com/en-us/library/ms186992.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Viktoria avatar image
Viktoria answered
Thank you very much!
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
@Viktoria : if an answer helps you please show this by accepting the answer (click the tick next to the answer you like)
0 Likes 0 ·
Oleg avatar image Oleg commented ·
@Kev Riley Actually, she should probably accept your answer because:
1. I answered later than you
2. My answer happens to be a replica of yours (not intended).
3. I disgracefully misspelled the word evil.
0 Likes 0 ·
Viktoria avatar image Viktoria commented ·
Both comments were very helpful. Thank you!
0 Likes 0 ·
Oleg avatar image Oleg commented ·
@Viktoria Thank you, but could you please mark Kevan's answer as accepted? (click the tick next to his answer). You are the only one who can do it because this is your question (other people don't see 'Accept' box). This allows other users of the site to find the answers quicker.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.