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

Home -> Community -> Usenet -> c.d.o.server -> Re: Simple problem...

Re: Simple problem...

From: DA Morgan <damorgan_at_exesolutions.com>
Date: Thu, 27 Feb 2003 14:33:29 -0800
Message-ID: <3E5E9238.AABAAB47@exesolutions.com>


Norman Dunbar wrote:

> Afternoon T,
>
> Unfortunately, I have had to roll my own function(s) to check - similar
> to the stuff below.
>
> I normally use TO_NUMBER in its own begin/exception/end block and trap
> the exceptions. Problem with Integer numbers is that they are also
> floats (so to speak) so is_a_float('100') should return true as would
> is_an_integer('100').
>
> In my version of IsAFloat, I allow the caller to specify whether
> integers are valid or not - the default is currently true, but can
> easily be changed to false.
>
> IsANumber() is not anything I've had to write - just call
> IsAFloat('whatever', true) instead.
>
> CREATE OR REPLACE FUNCTION IsAFloat(What IN VARCHAR2, IntIsFloat IN
> BOOLEAN DEFAULT TRUE)
> RETURN BOOLEAN
> AS
> -- ==============================================
> -- This function return 1 if the value passed in
> -- is a proper float representation. It will flag
> -- integers as floats unless told otherwise.
> -- ==============================================
> MyNumber NUMBER;
> MyOffset NUMBER := 1;
> BEGIN
> MyNumber := TO_NUMBER(What);
> IF (MyNumber = ROUND(MyNumber,0)) THEN
> -- This is an integer number
> IF (IntIsFloat) THEN
> -- Integers are allowed
> RETURN TRUE;
> ELSE
> -- Integers are not allowed
> RETURN FALSE;
> END IF;
> ELSE
> -- This is a float number
> RETURN TRUE;
> END IF;
> EXCEPTION
> WHEN VALUE_ERROR THEN RETURN FALSE;
> END;
> /
>
> CREATE OR REPLACE FUNCTION IsAnInteger(What IN VARCHAR2)
> RETURN BOOLEAN
> AS
> -- ==============================================
> -- This function returns 1 if the value passed in
> -- is a proper integer representation.
> -- ==============================================
> MyNumber NUMBER;
> BEGIN
> MyNumber := TO_NUMBER(What);
> IF (MyNumber = ROUND(MyNumber,0)) THEN
> RETURN TRUE;
> ELSE
> RETURN FALSE;
> END IF;
> EXCEPTION
> WHEN VALUE_ERROR THEN RETURN FALSE;
> END;
> /
>
> Might be useful - maybe ?
>
> Cheers,
> Norman.
>
> -------------------------------------
> Norman Dunbar
> Database/Unix administrator
> Lynx Financial Systems Ltd.
> mailto:Norman.Dunbar_at_LFS.co.uk
> Tel: 0113 289 6265
> Fax: 0113 289 3146
> URL: http://www.Lynx-FS.com
> -------------------------------------
>
> -----Original Message-----
> From: Telemachus [mailto:tollg_at_tendwa.rns.net]
> Posted At: Thursday, February 27, 2003 3:37 PM
> Posted To: server
> Conversation: Simple problem...
> Subject: Simple problem...
>
> But I can't find an easy solution.
>
> For 817 without rolling your own PL/SQL func...
> Is there a fast builtin way to do IS_A_NUMBER('STRING') or is_a_float()
> or
> is_an_integer()
>
> i.e. given a string return true if the string represents a valid number
> or
> false. Or do most people trap an error from TO_NUMBER ?
>
> T ...

How about modulus with 1?

If the result isn't zero ... it is a float.

Daniel Morgan Received on Thu Feb 27 2003 - 16:33:29 CST

Original text of this message

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