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

前端 未结 6 1190
渐次进展
渐次进展 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 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.

提交回复
热议问题