Results 1 to 10 of 10

Thread: [RESOLVED] Oracle Equivalent of IsNumeric

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Resolved [RESOLVED] Oracle Equivalent of IsNumeric

    I have this query I use against SQL Server that I need to execute against an Oracle database.

    I can't find an equivalent Oracle function for IsNumeric.

    Code:
    SELECT  
         CASE WHEN IsNumeric(SubStr(PERSON.NAME_FULL_FORMATTED,1,1)) = 1 Then  
            PERSON.LASTNAME + ', ' + PERSON.FIRSTNAME
         ELSE
            PERSON.NAME_FULL_FORMATTED
         END
    I use this web site for help on writing Oracle queries, maybe it is outdated.
    http://www.techonthenet.com/oracle/f...ndex_alpha.php
    Last edited by brucevde; Dec 7th, 2009 at 03:32 PM.

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Oracle Equivalent of IsNumeric

    Hi bruce,
    There is no easy way. You can use the translate function or make use of a regular expression to determine if it is numeric.

    The last trick is to create a function that will give you the SQL Server equivalent of ISNUMERIC in oracle.

    sql Code:
    1. create or replace
    2.   2  function isnumeric
    3.   3  ( p_string in varchar2)
    4.   4  return boolean
    5.   5  as
    6.   6      l_number number;
    7.   7  begin
    8.   8      l_number := p_string;
    9.   9      return TRUE;
    10.  10  exception
    11.  11      when others then
    12.  12          return FALSE;
    13.  13  end;
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Oracle Equivalent of IsNumeric

    I cannot create a procedure/function in the database so I used the Translate function. Thanks.

    Code:
    SELECT CASE WHEN
      TRANSLATE(SUBSTR(FULL_ENCNTR_PERSON.NAME_FULL_FORMATTED,1,1),'0123456789',' ') IS NULL THEN
      FULL_ENCNTR_PERSON.LASTNAME||', '||FULL_ENCNTR_PERSON.FIRSTNAME
      ELSE FULL_ENCNTR_PERSON.NAME_FULL_FORMATTED END ClientName

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    In any case, the regular expression is supported by 10G and above.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    bruce, zero length string is NULL but string full of spaces is not NULL. If version is 10g or higher then definitely use regex.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    They have Oracle 9.

    zero length string is NULL but string full of spaces is not NULL.
    ?? I am not sure what you are trying to say. The statement I posted works fine for my needs.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    Translate will replace the 1st character in the string_to_replace with the 1st character in the replacement_string. Then it will replace the 2nd character in the string_to_replace with the 2nd character in the replacement_string, and so on.

    If SUBSTR(FULL_ENCNTR_PERSON.NAME_FULL_FORMATTED,1,1 is '0' then result of translate is a space instead of expected NULL, only the other numbers are replace with nullstring since you didn't provide matching characters in replacement_string (only '0' is paired with ' ').

    If you will never encounter a leading zero in FULL_ENCNTR_PERSON.NAME_FULL_FORMATTED then yes your query will work but if there's data with leading '0' then your solution will fail.
    Last edited by leinad31; Dec 8th, 2009 at 02:55 AM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    After more testing (i found 1 record with a leading 0 - thanks leinad31) and searching I ended up using the following

    Code:
    SELECT CASE WHEN 
      LENGTH(TRANSLATE(SUBSTR(TRIM(NAME_FULL_FORMATTED),1,1), '+-.0123456789', ' ')) IS NULL THEN
      TRIM(LASTNAME||', '||FIRSTNAME)
    ELSE 
      TRIM(NAME_FULL_FORMATTED)
    END

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    If you want selected characters in your list to be replaced with nullstring and 3rd parameter is required by TRANSLATE then include a first char placeholder (not commonly used in data) to do a fake replace, such as "replace space with space".

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Oracle Equivalent of IsNumeric

    You can also try this and see if it is appropriate to your needs, just modify exception handling accordingly if you prefer to throw exceptions rather than an all or nothing approach. This also checks against specified precision and scale.

    Code:
    FUNCTION IS_NUMBER (p_value VARCHAR2, p_precision NUMBER DEFAULT NULL, p_scale NUMBER DEFAULT NULL)
          RETURN NUMBER
       AS
          func_ret    NUMBER;
    
          v_sql       VARCHAR2(4000) := 'SELECT CAST(:1 AS NUMBER <ps>) FROM DUAL';
    
          v_prevmod   VARCHAR2(100); -- V$SESSION.MODULE&#37;TYPE;
          v_prevact   VARCHAR2(100); -- V$SESSION.ACTION%TYPE;
          v_subprog   VARCHAR2(100) := 'IS_NUMBER';
       BEGIN
          DBMS_APPLICATION_INFO.Read_Module (v_prevmod, v_prevact);
          DBMS_APPLICATION_INFO.Set_Module  (PKG_NAME, v_subprog);
    
          BEGIN
             IF (p_precision IS NULL) AND (p_scale IS NULL) THEN
                func_ret := TO_NUMBER (p_value);
             ELSE
                IF (p_precision IS NULL) THEN
                   v_sql := REPLACE (v_sql, '<ps>', '(*,' || p_scale || ')');
                ELSIF (p_scale IS NULL) THEN
                   v_sql := REPLACE (v_sql, '<ps>', '(' || p_precision || ')');
                ELSE
                   v_sql := REPLACE (v_sql, '<ps>', '(' || p_precision || ',' || p_scale || ')');
                END IF;
    
                EXECUTE IMMEDIATE v_sql INTO func_ret USING p_value;
             END IF;
          EXCEPTION
          WHEN OTHERS THEN
             func_ret := NULL;
          END;
    Last edited by leinad31; Dec 9th, 2009 at 12:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width