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.
| 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 |
|
|
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.aspxSo your second expression is equivalent toWHERE ((Dib_Mvt_Typ = 'R' and LOREC4 like 'R%') or LOREC4 like 'S%' or LOREC4 like 'V%') |
 |
|
|
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 () |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
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 PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
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... |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-07-26 : 15:13:00
|
| Okay thanks for the clarification! |
 |
|
|
|
|
|