6 Replies Latest reply on Oct 18, 2016 9:13 AM by Mihaela Farcas

    AND vs OR

    Mihaela Farcas

      Hi, I would appreciate if someone could help me to understand what's the difference between AND vs OR in a calculated field

      i.e.

      AND

      [HCN Province] = "ON"

      OR

      [HCN Province] = "MB"

      OR

      [HCN Province] = "NB"

      and so on....

       

      vs

      AND

      [HCN Province] = "ON"

      AND

      [HCN Province] = "MB"

      AND

      [HCN Province] = "NB"

      .....

      also, if I include the statement in parenthesis it gives me a different result

      AND

      ([HCN Province] = "ON"

      OR

      [HCN Province] = "MB"

      OR

      [HCN Province] = "NB")

      I am really confused.

      Any help would be appreciated!

      Please and thank you very much!

        • 1. Re: AND vs OR
          David Li

          Hi Mihaela, this has to do with Boolean logic. If you need an introduction, this seems like a pretty good video on it: Boolean algebra #1: Basic laws and rules - YouTube

           

          Basically, AND and OR are logical keywords that combine multiple TRUE/FALSE statements.

           

          When you use AND between two statements, it means that both statements must be true for the whole thing to be true. For instance, if you only like animals that have two legs AND have wings, you like birds but not kangaroos.

           

          When you use OR between two statements, it means that either must be true for the whole thing to be true. If you only like animals that have two legs OR have wings, you like both birds and kangaroos.

           

          The parentheses have to do with grouping and order of operations. When you put parentheses around part of the expression, it makes that part inside the parentheses into its own mini-expression.

           

          For instance, let's say that your Boolean expression is that you like animals that:

           

               (Have Four Legs AND Have Antlers) OR Have Wings

           

          In this situation, the part in the parentheses has to be evaluated before the rest. For instance, antelope would be TRUE for that expression, so you like antelope. Also, birds are TRUE because they have wings (in the main expression). However, you would not like dogs, because they do not have both four legs AND antlers (nor do they have wings).

          • 2. Re: AND vs OR
            Wes Reneau

            Hi Mihaela,

             

            Try and think of it in terms of logical truth. AND requires multiple statements to be TRUE, while OR requires one of the conditions to be TRUE.

            1=1 is always True

            2=2 is always True

            0=1 is always False

             

            AND Example 1:

            if 1=1 and 0=1 then 'A' else 'B' end

            The statement above would return B, since 0=1 is false. The AND condition requires both conditions to be true.

            AND Example 2:

            if 1=1 and 2=2 then 'A' else 'B' end

            The statement above would return A, since 1=1 is True and 2=2 is True. Both conditions are true.

            OR Example 1:

            if 1=1 or 0=1 then 'A' else 'B' end

            The statement above would return A, since one of the conditions is True.OR simply requires one of the conditions to be True.

            Adding Parenthesis: Parenthesis simply require the logic to check the entire statement within the parenthesis

            if (1=1 and 2=2 OR 0=1) then 'A' else B end

            The statement above would return A since 1=1 AND 2=2. The 0=1 returns False, but since 1=1 and 2=2 is True it will still return A. The condition on the left side met the requirement of True.

             

            I hope this answers your question.

            -Wes

            • 3. Re: AND vs OR
              Mihaela Farcas

              Thank you very much David!

              that means if I I want to SELECT all patients from AB, ON, BC and so on, but not from Europe or other country, I use an OR? meaning give me any of these records

              I need all patients who are Canadian residents but no other countries.

              Please and thank you very much,

              1 of 1 people found this helpful
              • 4. Re: AND vs OR
                David Li

                Yes, you would use OR to link expressions that compare the patient province field to all the Canadian provinces. If you have a country field, that would be even easier, as you wouldn't need any custom Boolean logic.

                • 5. Re: AND vs OR
                  Mihaela Farcas

                  Got it

                  Thank you very much, I really appreciate

                  • 6. Re: AND vs OR
                    Mihaela Farcas

                    This is correct too if you use an if statement, Thank you very much