Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 HELP With -OR-

Author  Topic 

eskiuel
Starting Member

2 Posts

Posted - 2012-06-15 : 10:57:15
Hello,

I can see that you have a lot of knowledge in this forum, that's why I registered to get some help with this problem.

I'm trying to modify this code:
SQL7 = " FROM XXX_XX A, XX_XXBA B" & _
" WHERE A.LINENOM_NO=B.LINENOM_NO" & _
" AND GRADE='" & STATU & "'" & _
" AND TO_CHAR(B.UPDATE_DATE-7/24,'YYYYMMDD') BETWEEN '" & From_date & "' AND '" & To_date & "'" & _
" ORDER BY A.CUSTOMER"


By adding an OR to the code as follows:
SQL7 = " FROM XXX_XX A, XX_XXBA B" & _
" WHERE A.LINENOM_NO=B.LINENOM_NO" & _
" AND GRADE='" & STATU & "'" & _
" OR GRADE='" & STATD & "'" & _
" AND TO_CHAR(B.UPDATE_DATE-7/24,'YYYYMMDD') BETWEEN '" & From_date & "' AND '" & To_date & "'" & _
" ORDER BY A.CUSTOMER"


I'm trying to obtain the list of the products where the "GRADE" is STATU or STATD

But when I run the code it gives me this results but witout consider the Date Period.

Please, help me with the proper syntax.

It's difficult for me to write in english, but, thank you for your help.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-15 : 11:05:32
To account for operator precedence, you need to wrap the two OR conditions in a bracket as in:
SQL7 = " FROM XXX_XX A, XX_XXBA B" & _
" WHERE A.LINENOM_NO=B.LINENOM_NO" & _
" AND (GRADE='" & STATU & "'" & _
" OR GRADE='" & STATD & "')" & _
" AND TO_CHAR(B.UPDATE_DATE-7/24,'YYYYMMDD') BETWEEN '" & From_date & "' AND '" & To_date & "'" & _
" ORDER BY A.CUSTOMER"
SQL's precedence is listed here: http://msdn.microsoft.com/en-us/library/ms190276.aspx
Go to Top of Page

eskiuel
Starting Member

2 Posts

Posted - 2012-06-15 : 11:33:08
So if I understand in order to add more "OR" I need to put in brackets the code will be:

SQL7 = " FROM XXX_XX A, XX_XXBA B" & _
" WHERE A.LINENOM_NO=B.LINENOM_NO" & _
" AND (GRADE='" & STATU & "'" & _
" OR GRADE='" & STATD & "'" & _
" OR GRADE='" & STATT & "'" & _
" OR GRADE='" & STATC & "')" & _
" AND TO_CHAR(B.UPDATE_DATE-7/24,'YYYYMMDD') BETWEEN '" & From_date & "' AND '" & To_date & "'" & _
" ORDER BY A.CUSTOMER"
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-15 : 12:52:59
quote:
Originally posted by eskiuel

So if I understand in order to add more "OR" I need to put in brackets the code will be:

SQL7 = " FROM XXX_XX A, XX_XXBA B" & _
" WHERE A.LINENOM_NO=B.LINENOM_NO" & _
" AND (GRADE='" & STATU & "'" & _
" OR GRADE='" & STATD & "'" & _
" OR GRADE='" & STATT & "'" & _
" OR GRADE='" & STATC & "')" & _
" AND TO_CHAR(B.UPDATE_DATE-7/24,'YYYYMMDD') BETWEEN '" & From_date & "' AND '" & To_date & "'" & _
" ORDER BY A.CUSTOMER"


Yes. You are exactly right.
Go to Top of Page
   

- Advertisement -