<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: isNumber in sql</title>
	<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html</link>
	<description>Oracle Certified Master</description>
	<pubDate>Sun, 07 Sep 2008 06:17:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Giorgio Arata</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5724</link>
		<pubDate>Wed, 09 Jan 2008 15:17:59 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5724</guid>
					<description>You can find the code snippet copied + pasted to &lt;a href="http://oracle.pastebin.com/f15df5519" rel="nofollow"&gt;Oracle PasteBin&lt;/a&gt;.

For illustration purposes, a few test cases follow...

&lt;code&gt;
-- TRUE
EXEC IF is_numeric('0123') THEN DBMS_OUTPUT.PUT_LINE('TRUE'); END IF;
-- TRUE
EXEC IF is_numeric(0.123) THEN DBMS_OUTPUT.PUT_LINE('TRUE'); END IF;
-- TRUE
EXEC IF is_numeric('0.123') THEN DBMS_OUTPUT.PUT_LINE('TRUE'); END IF;
-- TRUE
EXEC IF is_numeric(123) THEN DBMS_OUTPUT.PUT_LINE('TRUE'); END IF;
-- TRUE
EXEC IF is_numeric('123') THEN DBMS_OUTPUT.PUT_LINE('TRUE'); END IF;
-- FALSE
EXEC IF NOT is_numeric('foo') THEN DBMS_OUTPUT.PUT_LINE('FALSE'); END IF;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>You can find the code snippet copied + pasted to <a href="http://oracle.pastebin.com/f15df5519" rel="nofollow">Oracle PasteBin</a>.</p>
<p>For illustration purposes, a few test cases follow&#8230;</p>
<p><pre><code>
-- TRUE
EXEC IF is_numeric(&#039;0123&#039;) THEN DBMS_OUTPUT.PUT_LINE(&#039;TRUE&#039;); END IF;
-- TRUE
EXEC IF is_numeric(0.123) THEN DBMS_OUTPUT.PUT_LINE(&#039;TRUE&#039;); END IF;
-- TRUE
EXEC IF is_numeric(&#039;0.123&#039;) THEN DBMS_OUTPUT.PUT_LINE(&#039;TRUE&#039;); END IF;
-- TRUE
EXEC IF is_numeric(123) THEN DBMS_OUTPUT.PUT_LINE(&#039;TRUE&#039;); END IF;
-- TRUE
EXEC IF is_numeric(&#039;123&#039;) THEN DBMS_OUTPUT.PUT_LINE(&#039;TRUE&#039;); END IF;
-- FALSE
EXEC IF NOT is_numeric(&#039;foo&#039;) THEN DBMS_OUTPUT.PUT_LINE(&#039;FALSE&#039;); END IF;
</code></pre>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Giorgio Arata</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5716</link>
		<pubDate>Tue, 08 Jan 2008 15:54:06 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5716</guid>
					<description>My approach on a 9i Rel2 DB is not corresponding with any of these..

FUNCTION is_numeric(number_str VARCHAR2) RETURN BOOLEAN IS
  x_is_numeric PLS_INTEGER := 0;
BEGIN
  SELECT XMLELEMENT ("ROWSET", 
  XMLAGG (
  XMLELEMENT("ROW", xmlattributes(ROWNUM AS "num"),
  XMLELEMENT ("STRING_VAL", REPLACE(number_str, ',', '.'))
   ))
   ).transform(XMLTYPE.createXML(
      '' &#124;&#124;
        '' &#124;&#124;
        '' &#124;&#124;
        '' &#124;&#124;
        '' &#124;&#124;
        ' ' &#124;&#124;
        '  ' &#124;&#124;
        ' ' &#124;&#124;
        '' &#124;&#124;
        '' &#124;&#124;
        '' &#124;&#124;
        '')).getStringVal() AS RES
    INTO x_is_numeric
    FROM dual;
    RETURN x_is_numeric &#62; 0;
END is_numeric;</description>
		<content:encoded><![CDATA[<p>My approach on a 9i Rel2 DB is not corresponding with any of these..</p>
<p>FUNCTION is_numeric(number_str VARCHAR2) RETURN BOOLEAN IS<br />
  x_is_numeric PLS_INTEGER := 0;<br />
BEGIN<br />
  SELECT XMLELEMENT (&#8221;ROWSET&#8221;,<br />
  XMLAGG (<br />
  XMLELEMENT(&#8221;ROW&#8221;, xmlattributes(ROWNUM AS &#8220;num&#8221;),<br />
  XMLELEMENT (&#8221;STRING_VAL&#8221;, REPLACE(number_str, &#8216;,&#8217;, &#8216;.&#8217;))<br />
   ))<br />
   ).transform(XMLTYPE.createXML(<br />
      &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216; &#8216; ||<br />
        &#8216;  &#8216; ||<br />
        &#8216; &#8216; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217; ||<br />
        &#8216;&#8217;)).getStringVal() AS RES<br />
    INTO x_is_numeric<br />
    FROM dual;<br />
    RETURN x_is_numeric &gt; 0;<br />
END is_numeric;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Laurent Schneider</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5581</link>
		<pubDate>Tue, 11 Dec 2007 11:03:50 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5581</guid>
					<description>+123.123E+2E-3 is not a valid number, it is a mathematical expression</description>
		<content:encoded><![CDATA[<p>+123.123E+2E-3 is not a valid number, it is a mathematical expression
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Wayne Phillips</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5580</link>
		<pubDate>Tue, 11 Dec 2007 08:36:29 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-5580</guid>
					<description>My results on a 10G Rel2 DB is not corresponding with the above example.
Please help me understand:
10:29:35 server-user@db&#62; select possible_num,
10:29:35   2         case when regexp_like(possible_num,'^[+-]?(\d+\.?&#124;\d*\.\d+)([eE][+-]\d+)?$')
10:29:35   3              then 'YES'
10:29:35   4         else 'NO'
10:29:35   5         end n
10:29:35   6  from t;

POSSIBLE_NUM         N
-------------------- ---
+123.123E+2E-3       NO --&#62; NOT a Valid Number ???
-abcde               NO
123                  YES
-1.2e-3              YES
abc                  NO
-123                 YES
+123                 YES
-123.123             YES
+123.123             YES
-123.123E-23         YES
+123.123E+2          YES

11 rows selected.

10:29:35 server-user@db&#62; select possible_num,
10:31:58   2         case when regexp_like(possible_num,'^[+-]?(\d+\.?&#124;\d*\.\d+)([eE][+-]\d+)?$')
10:31:58   3              then to_number(possible_num)
10:31:58   4         end n
10:31:58   5  from t;

POSSIBLE_NUM                  N
-------------------- ----------
+123.123E+2E-3                  --&#62; NOT a Valid Number ???
-abcde
123                         123
-1.2e-3                  -.0012
abc
-123                       -123
+123                        123
-123.123               -123.123
+123.123                123.123
-123.123E-23         -1.231E-21
+123.123E+2             12312.3

11 rows selected.</description>
		<content:encoded><![CDATA[<p>My results on a 10G Rel2 DB is not corresponding with the above example.<br />
Please help me understand:<br />
10:29:35 <a href="mailto:server-user@db&gt;">server-user@db&gt;</a> select possible_num,<br />
10:29:35   2         case when regexp_like(possible_num,&#8217;^[+-]?(\d+\.?|\d*\.\d+)([eE][+-]\d+)?$&#8217;)<br />
10:29:35   3              then &#8216;YES&#8217;<br />
10:29:35   4         else &#8216;NO&#8217;<br />
10:29:35   5         end n<br />
10:29:35   6  from t;</p>
<p>POSSIBLE_NUM         N<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;<br />
+123.123E+2E-3       NO &#8211;&gt; NOT a Valid Number ???<br />
-abcde               NO<br />
123                  YES<br />
-1.2e-3              YES<br />
abc                  NO<br />
-123                 YES<br />
+123                 YES<br />
-123.123             YES<br />
+123.123             YES<br />
-123.123E-23         YES<br />
+123.123E+2          YES</p>
<p>11 rows selected.</p>
<p>10:29:35 <a href="mailto:server-user@db&gt;">server-user@db&gt;</a> select possible_num,<br />
10:31:58   2         case when regexp_like(possible_num,&#8217;^[+-]?(\d+\.?|\d*\.\d+)([eE][+-]\d+)?$&#8217;)<br />
10:31:58   3              then to_number(possible_num)<br />
10:31:58   4         end n<br />
10:31:58   5  from t;</p>
<p>POSSIBLE_NUM                  N<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;-<br />
+123.123E+2E-3                  &#8211;&gt; NOT a Valid Number ???<br />
-abcde<br />
123                         123<br />
-1.2e-3                  -.0012<br />
abc<br />
-123                       -123<br />
+123                        123<br />
-123.123               -123.123<br />
+123.123                123.123<br />
-123.123E-23         -1.231E-21<br />
+123.123E+2             12312.3</p>
<p>11 rows selected.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Laurent Schneider</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4782</link>
		<pubDate>Tue, 18 Sep 2007 13:39:10 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4782</guid>
					<description>ok, +1 is  a valid number, good point

I also forget that 1e10 is valid too, so the expression will be
&lt;i&gt;
regexp_like(:x, ‘^[+-]?(&#92;d+&#92;.?&#124;&#92;d*&#92;.&#92;d+)([eE][+-]?&#92;d+)?$’)
&lt;/i&gt;</description>
		<content:encoded><![CDATA[<p>ok, +1 is  a valid number, good point</p>
<p>I also forget that 1e10 is valid too, so the expression will be<br />
<i><br />
regexp_like(:x, ‘^[+-]?(&#92;d+&#92;.?|&#92;d*&#92;.&#92;d+)([eE][+-]?&#92;d+)?$’)<br />
</i>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Delfino</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4781</link>
		<pubDate>Tue, 18 Sep 2007 13:31:07 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4781</guid>
					<description>I used the following option, but not for scientific numbers and after introducing it fails.


SQL&#62; select col2
  2        ,decode (length (translate (trim (col2), ' +-.0123456789eE', ' ') )
  3                ,null, 'Yes'
  4                ,'No') is_number
  5    from t;

COL2                           IS_
------------------------------ ---
+123.123E+2E-3                 Yes
-abcde                         No
123                            Yes
-1.2e-3                        Yes
abc                            No
-123                           Yes
+123                           Yes
-123.123                       Yes
+123.123                       Yes
-123.123E-23                   Yes
+123.123E+2                    Yes

11 rows selected.



I like your options, I just changed a little bit the regexp solution to include the (+) plus sign "[+-]"

SQL&#62; ed
Wrote file afiedt.buf

  1  select col2,
  2    case when
  3      regexp_like(col2,
  4        '^[+-]?(\d+\.?&#124;\d*\.\d+)([eE][+-]\d+)?$')
  5      then to_number(col2)
  6    end n
  7* from t
SQL&#62; /

COL2                                    N
------------------------------ ----------
+123.123E+2E-3
-abcde
123                                   123
-1.2e-3                            -.0012
abc
-123                                 -123
+123                                  123
-123.123                         -123.123
+123.123                          123.123
-123.123E-23                   -1.231E-21
+123.123E+2                       12312.3

11 rows selected.
</description>
		<content:encoded><![CDATA[<p>I used the following option, but not for scientific numbers and after introducing it fails.</p>
<p>SQL&gt; select col2<br />
  2        ,decode (length (translate (trim (col2), &#8216; +-.0123456789eE&#8217;, &#8216; &#8216;) )<br />
  3                ,null, &#8216;Yes&#8217;<br />
  4                ,&#8217;No&#8217;) is_number<br />
  5    from t;</p>
<p>COL2                           IS_<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;<br />
+123.123E+2E-3                 Yes<br />
-abcde                         No<br />
123                            Yes<br />
-1.2e-3                        Yes<br />
abc                            No<br />
-123                           Yes<br />
+123                           Yes<br />
-123.123                       Yes<br />
+123.123                       Yes<br />
-123.123E-23                   Yes<br />
+123.123E+2                    Yes</p>
<p>11 rows selected.</p>
<p>I like your options, I just changed a little bit the regexp solution to include the (+) plus sign &#8220;[+-]&#8221;</p>
<p>SQL&gt; ed<br />
Wrote file afiedt.buf</p>
<p>  1  select col2,<br />
  2    case when<br />
  3      regexp_like(col2,<br />
  4        &#8216;^[+-]?(\d+\.?|\d*\.\d+)([eE][+-]\d+)?$&#8217;)<br />
  5      then to_number(col2)<br />
  6    end n<br />
  7* from t<br />
SQL&gt; /</p>
<p>COL2                                    N<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;&#8212;-<br />
+123.123E+2E-3<br />
-abcde<br />
123                                   123<br />
-1.2e-3                            -.0012<br />
abc<br />
-123                                 -123<br />
+123                                  123<br />
-123.123                         -123.123<br />
+123.123                          123.123<br />
-123.123E-23                   -1.231E-21<br />
+123.123E+2                       12312.3</p>
<p>11 rows selected.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Laurent Schneider</title>
		<link>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4780</link>
		<pubDate>Tue, 18 Sep 2007 11:20:52 +0000</pubDate>
		<guid>http://laurentschneider.com/wordpress/2007/09/isnumber-in-sql.html#comment-4780</guid>
					<description>to check if it is numeric 
&lt;code&gt;
select x, 
  xmlquery('let $i:=if($X castable as xs:decimal)then "YES" else "NO" return $i' 
    passing x as x 
    returning content) isnumeric 
from t;
X       ISNUMERIC
------- ---------
123     YES
-1.2e-3 YES
abc     NO
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>to check if it is numeric<br />
<pre><code>
select x, 
&nbsp;&nbsp;xmlquery(&#039;let $i:=if($X castable as xs:decimal)then &quot;YES&quot; else &quot;NO&quot; return $i&#039; 
&nbsp;&nbsp;&nbsp;&nbsp;passing x as x 
&nbsp;&nbsp;&nbsp;&nbsp;returning content) isnumeric 
from t;
X&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISNUMERIC
------- ---------
123&nbsp;&nbsp;&nbsp;&nbsp; YES
-1.2e-3 YES
abc&nbsp;&nbsp;&nbsp;&nbsp; NO
</code></pre>
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
