SQLTeam.com | Weblogs | Forums

Trying to understan an, AND - OR statement

Hi.
I'm not sure I'm understanding this correctly, so I need some help

WHERE	((ISNULL(T.TransT_strBarcodeRedemp,'') <> '' AND ISNULL(T.TransT_strVoucherBarcode,'') = '')
		OR TS.HOPK in ('S','T','U','V','W','X','Y') OR TickNum_strStyleCode = '|TICK|S|N|N|N|' OR TickNum_strStyleCode = '|CONC|S|N|N|T|')
		 and not exists(select 1 from zz_PaidVoucher PV where  PV.VoucherCode = LEFT(ISNULL(TransT_strBarcodeRedemp,''),4) or PV.VoucherCode = LEFT(ISNULL(TransT_strVoucherBarcode,''),4)) 
		) T

From what I understand the this will split into 3 parts. One with the first "AND" in parenthesis
(ISNULL(T.TransT_strBarcodeRedemp,'') <> '' AND ISNULL(T.TransT_strVoucherBarcode,'') = '')
then to The three OR's and then to the final not exist.
So the First parenthesis "AND" part will take place only if it finds a match, if not then the OR's will take part and if nothing found on the OR's then the not exists does not really do something.

If that is correct then this is a wrong script that I need to change. Can you let me know if my explanation is correct?

Thanks.

Not exactly.

If you format the query, it becomes more visible

WHERE ((ISNULL(T.TransT_strBarcodeRedemp,'') <> ''
  AND   ISNULL(T.TransT_strVoucherBarcode,'') = ''
       )
   OR  TS.HOPK in ('S','T','U','V','W','X','Y')
   OR  TickNum_strStyleCode = '|TICK|S|N|N|N|'
   OR  TickNum_strStyleCode = '|CONC|S|N|N|T|'
      )
  and not exists(select 1
                   from zz_PaidVoucher PV
                  where PV.VoucherCode = LEFT(ISNULL(TransT_strBarcodeRedemp,''),4)
                     or PV.VoucherCode = LEFT(ISNULL(TransT_strVoucherBarcode,''),4)
                )
 ) T

The "not exists" section should ALWAYS be true.

Hi.Yes I probably should not wrote the not exist part.Please ignore it.
I'm more interested if my assumption for the parenthesis "AND" and the 3 OR's are true.

Thanks.

True

1 Like

Hmm.
If so I need to change the code.
Thanks.

Well if the and section is true or one of the or sections are true.
Was that what you meant?

Yes.
The Or sessions must be activated only if the AND session is true so I must probably use a Case or If scenario
Thanks