Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: Convert character string to number

RE: Convert character string to number

From: Jacques Kilchoer <Jacques.Kilchoer_at_quest.com>
Date: Wed, 24 Jan 2001 19:28:03 -0800
Message-Id: <10751.127431@fatcity.com>


This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible.

------_=_NextPart_001_01C0867E.D346AEC0
Content-Type: text/plain;

        charset="iso-8859-1"

see coments at end of e-mail.

> -----Original Message-----
> From: Helmut Daiminger [mailto:hdaiminger_at_vivonet.com]
> Sent: mercredi, 24. janvier 2001 16:41
>
> I want to read a column (varchar2) from a table and convert
> the contents
> into numbers if the string consists of numbers only. If the
> string contains
> characters, I don't want to convert it.
>
> Example for data in varchar2 colum:
>
> row 1: 12345
> row 2: text
> row 3: 123dfe
> row 4: 9876432
>
> I can easily conver row 1 and 4 using the to_number function,
> right? But how
> can I tell Oracle to skip rows 2 and 3 since the character field also
> contains characters not just numbers?

You could try this:

SQL> select v from x ;

V



12345
text
123dfe
9876432

SQL> select decode (translate (v, '~1234567890', '~'), null, to_number (v), null)
  2 from x ;

DECODE(TRANSLATE(V,'~1234567890','~'),NULL,TO_NUMBER(V),NULL)


                                                        12345
NULL VALUE
NULL VALUE
                                                      9876432



any ignorant comments made are the sole responsibility of J. R. Kilchoer and should not reflect adversely upon my employer.  

Jacques R. Kilchoer
(949) 754-8816
Quest Software, Inc.
8001 Irvine Center Drive
Irvine, California 92618
U.S.A.
http://www.quest.com

------_=_NextPart_001_01C0867E.D346AEC0
Content-Type: text/html;

        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version = 5.5.2652.35">
<TITLE>RE: Convert character string to number</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>see coments at end of e-mail.</FONT> </P>

<P><FONT SIZE=3D2>&gt; -----Original Message-----</FONT> <BR><FONT SIZE=3D2>&gt; From: Helmut Daiminger [<A = HREF=3D"mailto:hdaiminger_at_vivonet.com">mailto:hdaiminger_at_vivonet.com</A>= ]</FONT>

<BR><FONT SIZE=3D2>&gt; Sent: mercredi, 24. janvier 2001 16:41</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I want to read a column (varchar2) from a table =
and convert </FONT>

<BR><FONT SIZE=3D2>&gt; the contents</FONT> <BR><FONT SIZE=3D2>&gt; into numbers if the string consists of numbers = only. If the </FONT>
<BR><FONT SIZE=3D2>&gt; string contains</FONT>
<BR><FONT SIZE=3D2>&gt; characters, I don't want to convert it.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Example for data in varchar2 colum:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; row 1:&nbsp;&nbsp; 12345</FONT>
<BR><FONT SIZE=3D2>&gt; row 2:&nbsp;&nbsp; text</FONT>
<BR><FONT SIZE=3D2>&gt; row 3:&nbsp;&nbsp; 123dfe</FONT>
<BR><FONT SIZE=3D2>&gt; row 4:&nbsp;&nbsp; 9876432</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I can easily conver row 1 and 4 using the =
to_number function, </FONT>
<BR><FONT SIZE=3D2>&gt; right? But how</FONT> <BR><FONT SIZE=3D2>&gt; can I tell Oracle to skip rows 2 and 3 since = the character field also</FONT>
<BR><FONT SIZE=3D2>&gt; contains characters not just numbers?</FONT> </P>

<P><FONT SIZE=3D2>You could try this:</FONT> </P>

<P><FONT SIZE=3D2>SQL&gt; select v from x ;</FONT> </P>

<P><FONT SIZE=3D2>V</FONT>
<BR><FONT SIZE=3D2>----------</FONT>
<BR><FONT SIZE=3D2>12345</FONT>
<BR><FONT SIZE=3D2>text</FONT>
<BR><FONT SIZE=3D2>123dfe</FONT>
<BR><FONT SIZE=3D2>9876432</FONT>
</P>

<P><FONT SIZE=3D2>SQL&gt;&nbsp; select decode (translate (v, = '~1234567890', '~'), null, to_number (v), null)</FONT> <BR><FONT SIZE=3D2>&nbsp; 2&nbsp; from x ;</FONT> </P>

<P><FONT =
SIZE=3D2>DECODE(TRANSLATE(V,'~1234567890','~'),NULL,TO_NUMBER(V),NULL)</= FONT>
<BR><FONT =

SIZE=3D2>-------------------------------------------------------------</=
FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12345</FONT>
<BR><FONT SIZE=3D2>NULL VALUE</FONT>

<BR><FONT SIZE=3D2>NULL VALUE</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9876432</FONT>
</P>

<P><FONT SIZE=3D2>------</FONT>
<BR><FONT SIZE=3D2>any ignorant comments made are the sole = responsibility of J. R. Kilchoer and should not reflect adversely upon = my employer.</FONT></P>

<P><FONT SIZE=3D2>&nbsp;</FONT>
<BR><FONT SIZE=3D2>Jacques R. Kilchoer</FONT>
<BR><FONT SIZE=3D2>(949) 754-8816</FONT>
<BR><FONT SIZE=3D2>Quest Software, Inc.</FONT>
<BR><FONT SIZE=3D2>8001 Irvine Center Drive</FONT>
<BR><FONT SIZE=3D2>Irvine, California 92618</FONT>
<BR><FONT SIZE=3D2>U.S.A.</FONT>
<BR><FONT SIZE=3D2><A HREF=3D"http://www.quest.com" =
Received on Wed Jan 24 2001 - 21:28:03 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US