Received: (qmail 32102 invoked from network); 9 Sep 2009 14:02:51 -0500
Received: from freelists-180.iquest.net (HELO turing.freelists.org) (206.53.239.180)
  by static-ip-85-25-126-90.inaddr.intergenia.de with SMTP; 9 Sep 2009 14:02:44 -0500
Received: from localhost (localhost [127.0.0.1])
 by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 007DBCADDBD;
 Wed,  9 Sep 2009 15:02:41 -0400 (EDT)
X-Virus-Scanned: Debian amavisd-new at localhost.localdomain
Received: from turing.freelists.org ([127.0.0.1])
 by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id BbdEG4YWUetk; Wed,  9 Sep 2009 15:02:41 -0400 (EDT)
Received: from turing.freelists.org (localhost [127.0.0.1])
 by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id E02D7CADCB6;
 Wed,  9 Sep 2009 15:02:03 -0400 (EDT)
Received: with ECARTIS (v1.0.0; list oracle-l); Wed, 09 Sep 2009 15:01:22 -0400 (EDT)
Received: from localhost (localhost [127.0.0.1])	by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id ABF41CADB66	for <oracle-l@freelists.org>; Wed,  9 Sep 2009 15:01:22 -0400 (EDT)
Received: from turing.freelists.org ([127.0.0.1])	by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024)	with ESMTP id z9l8GBBq1GD4 for <oracle-l@freelists.org>;	Wed,  9 Sep 2009 15:01:22 -0400 (EDT)
Received: from mail-px0-f185.google.com (mail-px0-f185.google.com [209.85.216.185])	by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 38DD5CACF65	for <oracle-l@freelists.org>; Wed,  9 Sep 2009 15:01:21 -0400 (EDT)
Received: by pxi15 with SMTP id 15so341361pxi.25        for <oracle-l@freelists.org>; Wed, 09 Sep 2009 12:01:21 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.142.4.39 with SMTP id 39mr40988wfd.128.1252522881402; Wed, 09 	Sep 2009 12:01:21 -0700 (PDT)
In-Reply-To: <35B30104-23D2-42B9-9CC1-C535BB0CCA59@gmail.com>
References: <ff320f68e536ea40875046cf06a2794d.squirrel@society.servebeer.com>	 <26fdee6e0909081635h4b1a1562wbf5d51e3d5635461@mail.gmail.com>	 <C970F08BBE1E164AA8063E01502A71CF0154F692@WIN02.hotsos.com>	 <26fdee6e0909082219r3dc6efb0lfaf5a4db0f3a3fdb@mail.gmail.com>	 <35B30104-23D2-42B9-9CC1-C535BB0CCA59@gmail.com>
Date: Thu, 10 Sep 2009 03:01:21 +0800
Message-ID: <4602f23c0909091201m3dbe811fkd56e3abe49ede640@mail.gmail.com>
Subject: Re: Operator and condition precedence
From: Tanel Poder <tanel@poderc.com>
To: martin.a.berger@gmail.com
Cc: michaeljmoore@gmail.com, Ric Van Dyke <ric.van.dyke@hotsos.com>, 	rjoralist@society.servebeer.com, Oracle L <oracle-l@freelists.org>
Content-Type: multipart/alternative; boundary=00504502af826b46d7047329b5dd
X-archive-position: 20689
X-ecartis-version: Ecartis v1.0.0
Sender: oracle-l-bounce@freelists.org
Errors-to: oracle-l-bounce@freelists.org
X-original-sender: tanel@poderc.com
Precedence: normal
Reply-to: tanel@poderc.com
List-help: <mailto:ecartis@freelists.org?Subject=help>
List-unsubscribe: <oracle-l-request@freelists.org?Subject=unsubscribe>
List-software: Ecartis version 1.0.0
List-Id: oracle-l <oracle-l.freelists.org>
X-List-ID: oracle-l <oracle-l.freelists.org>
List-subscribe: <oracle-l-request@freelists.org?Subject=subscribe>
List-owner: <mailto:steve.adams@ixora.com.au>
List-post: <mailto:oracle-l@freelists.org>
List-archive: <http://www.freelists.org/archives/oracle-l>
X-list: oracle-l
--00504502af826b46d7047329b5dd
Content-Type: text/plain; charset=ISO-8859-1

Empty string is treated as NULL in Oracle. And you can compare where varchar
is bigger than NULL.

Once the first predicate evaluated returns false, there is no need to go and
check the next predicate in AND condition as the end result would be false
anyway. Thus we never really try to apply to_number() to dummy column.

Here's your example:

SQL> select * from dual where dummy > '' and to_number(dummy) > 0;

no rows selected

Here's your example with predicates switched around, but Oracle still uses
the direct comparison one first and never tries the to_number one:

SQL> select * from dual where to_number(dummy) > 0 and dummy > '';

no rows selected


But I can force my order of predicates so that to_number(dummy) one would be
evaluated first:

SQL> select /*+ *ordered_predicates* */ * from dual where to_number(dummy) >
0 and dummy > '';
select /*+ ordered_predicates */ * from dual where to_number(dummy) > 0 and
dummy > ''
                                                   *
ERROR at line 1:
ORA-01722: invalid number


--
Tanel Poder
http://blog.tanelpoder.com


On Thu, Sep 10, 2009 at 2:33 AM, Martin Berger <martin.a.berger@gmail.com>wrote:

> Mike,
> does not play it too easy:
>
> select * from dual where 'xyz'>'' and to_number('abc') > 0
>                                                 *
> ERROR at line 1:
> ORA-01722: invalid number
>
>
> SQL> select * from dual where dummy > '' and to_number(dummy) > 0;
>
>
> no rows selected
>
> (10.2.0.4 on Sunos10 - 2nodeRAC)
>
> seems to be something for deeper investigating ;-)
>
> Martin
>
>
>
> Am 09.09.2009 um 07:19 schrieb Michael Moore:
>
> Ric,
> On my system
>
>>
>>
>> select * from dual
>> where 'xyz'>'' and to_number('abc') > 0;
>>
> throws a numeric error
>
>
>>
>> Mike
>>
>>
>>
>
>
>


-- 
Tanel Poder
http://blog.tanelpoder.com

--00504502af826b46d7047329b5dd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Empty string is treated as NULL in Oracle. And you can compare where varcha=
r is bigger than NULL.<br><br>Once the first predicate evaluated returns fa=
lse, there is no need to go and check the next predicate in AND condition a=
s the end result would be false anyway. Thus we never really try to apply t=
o_number() to dummy column.<br>
<br>Here&#39;s your example:<br><br><span style=3D"font-family: courier new=
,monospace;">SQL&gt; select * from dual where dummy &gt; &#39;&#39; and to_=
number(dummy) &gt; 0;</span><br style=3D"font-family: courier new,monospace=
;">
<br style=3D"font-family: courier new,monospace;"><span style=3D"font-famil=
y: courier new,monospace;">no rows selected</span><br style=3D"font-family:=
 courier new,monospace;"><span style=3D"font-family: courier new,monospace;=
"></span><br style=3D"font-family: courier new,monospace;">
Here&#39;s your example with predicates switched around, but Oracle still u=
ses the direct comparison one first and never tries the to_number one:<br>
<span style=3D"font-family: courier new,monospace;"><br>SQL&gt; select * fr=
om dual where to_number(dummy) &gt; 0 and dummy &gt; &#39;&#39;;</span><br =
style=3D"font-family: courier new,monospace;"><br style=3D"font-family: cou=
rier new,monospace;">
<span style=3D"font-family: courier new,monospace;">no rows selected</span>=
<br style=3D"font-family: courier new,monospace;"><br><br>But I can force m=
y order of predicates so that to_number(dummy) one would be evaluated first=
:<br>

<br style=3D"font-family: courier new,monospace;"><span style=3D"font-famil=
y: courier new,monospace;">SQL&gt; select /*+ <b>ordered_predicates</b> */ =
* from dual where to_number(dummy) &gt; 0 and dummy &gt; &#39;&#39;;</span>=
<br style=3D"font-family: courier new,monospace;">
<span style=3D"font-family: courier new,monospace;">select /*+ ordered_pred=
icates */ * from dual where to_number(dummy) &gt; 0 and dummy &gt; &#39;&#3=
9;</span><br style=3D"font-family: courier new,monospace;"><span style=3D"f=
ont-family: courier new,monospace;">=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 *</span><br style=3D"font-family: cour=
ier new,monospace;">
<span style=3D"font-family: courier new,monospace;">ERROR at line 1:</span>=
<br style=3D"font-family: courier new,monospace;"><span style=3D"font-famil=
y: courier new,monospace;">ORA-01722: invalid number</span><br style=3D"fon=
t-family: courier new,monospace;">
<br><br>--<br>Tanel Poder<br><a href=3D"http://blog.tanelpoder.com">http://=
blog.tanelpoder.com</a><br><br><br><div class=3D"gmail_quote">On Thu, Sep 1=
0, 2009 at 2:33 AM, Martin Berger <span dir=3D"ltr">&lt;<a href=3D"mailto:m=
artin.a.berger@gmail.com">martin.a.berger@gmail.com</a>&gt;</span> wrote:<b=
r>
<blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, =
204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style=3D"wor=
d-wrap: break-word;">Mike,<div><br></div><div>does not play it too easy:</d=
iv>
<div><br></div><div><div class=3D"im"><div>select * from dual where &#39;xy=
z&#39;&gt;&#39;&#39; and to_number(&#39;abc&#39;) &gt; 0</div></div><div cl=
ass=3D"im"><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*</div><div>
ERROR at line 1:</div><div>ORA-01722: invalid number</div><div><br></div><d=
iv><br></div></div><div>SQL&gt; select * from dual where dummy &gt; &#39;&#=
39; and to_number(dummy) &gt; 0; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0</div>
<div><br></div><div>no rows selected</div><div><br></div><div>(10.2.0.4 on =
Sunos10 - 2nodeRAC)</div><div><br></div><div>seems to be something for deep=
er investigating ;-)</div><div><br></div><div>Martin</div></div><div><br>
<div> <span style=3D"font-size: 12px;"><div style=3D"word-wrap: break-word;=
"><div><div><span style=3D"font-size: medium;"><br></span></div><div><span =
style=3D"font-size: medium;"><br></span></div></div></div></span></div><div=
><div>
Am 09.09.2009 um 07:19 schrieb Michael Moore:</div><div><div></div><div cla=
ss=3D"h5"><br><blockquote type=3D"cite">Ric,<br>On my system<br><font size=
=3D"3"><font face=3D"Times New Roman">=A0</font></font><div class=3D"gmail_=
quote"><blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rg=
b(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
 <div link=3D"blue" vlink=3D"blue" lang=3D"EN-US"><div><div><div><p style=
=3D"margin-bottom: 12pt;"><font face=3D"Times New Roman" size=3D"3"><span s=
tyle=3D"font-size: 12pt;"> <br> select * from dual<br> where &#39;xyz&#39;&=
gt;&#39;&#39; and to_number(&#39;abc&#39;) &gt; 0;<br>
</span></font></p></div></div></div></div></blockquote><div>throws a numeri=
c error <br></div><div>=A0</div><blockquote class=3D"gmail_quote" style=3D"=
border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; paddi=
ng-left: 1ex;">
 <div link=3D"blue" vlink=3D"blue" lang=3D"EN-US"><div><div><div><p style=
=3D"margin-bottom: 12pt;"><font face=3D"Times New Roman" size=3D"3"><span s=
tyle=3D"font-size: 12pt;"><br> Mike</span></font></p> <div><font face=3D"Ti=
mes New Roman" size=3D"3"><span style=3D"font-size: 12pt;"><font color=3D"#=
888888"><span style=3D"color: rgb(136, 136, 136);"><br>
 </span></font></span></font> </div><div><font face=3D"Times New Roman" siz=
e=3D"3"><span style=3D"font-size: 12pt;">=A0</span></font><br></div> </div>=
</div></div> </div> </blockquote></div><br></blockquote></div></div></div><=
br></div>
</div></blockquote></div><br><br clear=3D"all"><br>-- <br>Tanel Poder<br><a=
 href=3D"http://blog.tanelpoder.com">http://blog.tanelpoder.com</a><br><br>

--00504502af826b46d7047329b5dd--
--
http://www.freelists.org/webpage/oracle-l


