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
 Stored Procedure Question

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-07-26 : 14:45:00
Why would I get different results for these two where clauses:

WHERE (Dib_Mvt_Typ = 'R') and (LOREC4 like 'R%' or LOREC4 like 'S%' or LOREC4 like 'V%')


WHERE (Dib_Mvt_Typ = 'R' and LOREC4 like 'R%' or LOREC4 like 'S%' or LOREC4 like 'V%')


Which one is correct?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-26 : 14:47:45
Which one is correct is dependent your business requirement. They are different because parenthesis matter with AND/OR.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-26 : 14:49:36
This is because of the way in which the operator precedence in SQL Server works. AND has a higher precedence. Take a look here: http://msdn.microsoft.com/en-us/library/ms190276.aspx

So your second expression is equivalent to

WHERE ((Dib_Mvt_Typ = 'R' and LOREC4 like 'R%') or LOREC4 like 'S%' or LOREC4 like 'V%')
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-07-26 : 14:49:39
I want to get records where dib_mvt_typ='r' and lorec4 like etc...

Where would I put the ()
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-26 : 14:52:19
The first one appears to be the one you want based upon your short description of what you want.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-07-26 : 14:54:03
They are totally different search conditions.
AND has a higher precedence than OR. NOT has a higher precedence than AND.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-07-26 : 14:54:51
Thanks is it because I want to get the div_mvt_typ='r' first then make sure the Lorec4 is like etc...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-26 : 14:57:48
It doesn't have to do with "first". It has to do with you needing both to be true, hence AND. And then within your Lorec4 part, you want any of them, hence OR. AND takes precedence. This is not specific to SQL Server. This is a general programming concept.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-07-26 : 15:13:00
Okay thanks for the clarification!
Go to Top of Page
   

- Advertisement -