How can you tell if a value is not numeric in Oracle?

前端 未结 6 1169
渐次进展
渐次进展 2020-11-27 05:37

I have the following code that returns an error message if my value is invalid. I would like to give the same error message if the value given is not numeric.



        
相关标签:
6条回答
  • 2020-11-27 05:59

    The best answer I found on internet:

    SELECT case when trim(TRANSLATE(col1, '0123456789-,.', ' ')) is null
                then 'numeric'
                else 'alpha'
           end
    FROM tab1;
    
    0 讨论(0)
  • 2020-11-27 06:01
    REGEXP_LIKE(column, '^[[:digit:]]+$')
    

    returns TRUE if column holds only numeric characters

    0 讨论(0)
  • 2020-11-27 06:03

    There is no built-in function. You could write one

    CREATE FUNCTION is_numeric( p_str IN VARCHAR2 )
      RETURN NUMBER
    IS
      l_num NUMBER;
    BEGIN
      l_num := to_number( p_str );
      RETURN 1;
    EXCEPTION
      WHEN value_error
      THEN
        RETURN 0;
    END;
    

    and/or

    CREATE FUNCTION my_to_number( p_str IN VARCHAR2 )
      RETURN NUMBER
    IS
      l_num NUMBER;
    BEGIN
      l_num := to_number( p_str );
      RETURN l_num;
    EXCEPTION
      WHEN value_error
      THEN
        RETURN NULL;
    END;
    

    You can then do

    IF( is_numeric( str ) = 1 AND 
        my_to_number( str ) >= 1000 AND
        my_to_number( str ) <= 7000 )
    

    If you happen to be using Oracle 12.2 or later, there are enhancements to the to_number function that you could leverage

    IF( to_number( str default null on conversion error ) >= 1000 AND
        to_number( str default null on conversion error ) <= 7000 )
    
    0 讨论(0)
  • 2020-11-27 06:08

    From Oracle DB 12c Release 2 you could use VALIDATE_CONVERSION function:

    VALIDATE_CONVERSION determines whether expr can be converted to the specified data type. If expr can be successfully converted, then this function returns 1; otherwise, this function returns 0. If expr evaluates to null, then this function returns 1. If an error occurs while evaluating expr, then this function returns the error.

     IF (VALIDATE_CONVERSION(value AS NUMBER) = 1) THEN
         ...
     END IF;
    

    db<>fiddle demo

    0 讨论(0)
  • 2020-11-27 06:11

    You can use the following regular expression which will match integers (e.g., 123), floating-point numbers (12.3), and numbers with exponents (1.2e3):

    ^-?\d*\.?\d+([eE]-?\d+)?$
    

    If you want to accept + signs as well as - signs (as Oracle does with TO_NUMBER()), you can change each occurrence of - above to [+-]. So you might rewrite your block of code above as follows:

    IF (option_id = 0021) THEN 
        IF NOT REGEXP_LIKE(value, '^[+-]?\d*\.?\d+([eE][+-]?\d+)?$') OR TO_NUMBER(value) < 10000 OR TO_NUMBER(value) > 7200000 THEN
            ip_msg(6214,option_name);
            RETURN;
        END IF;
    END IF;
    

    I am not altogether certain that would handle all values so you may want to add an EXCEPTION block or write a custom to_number() function as @JustinCave suggests.

    0 讨论(0)
  • 2020-11-27 06:19
    CREATE OR REPLACE FUNCTION IS_NUMERIC(P_INPUT IN VARCHAR2) RETURN INTEGER IS
      RESULT INTEGER;
      NUM NUMBER ;
    BEGIN
      NUM:=TO_NUMBER(P_INPUT);
      RETURN 1;
    EXCEPTION WHEN OTHERS THEN
      RETURN 0;
    END IS_NUMERIC;
    /
    
    0 讨论(0)
提交回复
热议问题