Evaluate String Expression
-- The PL/SQL custom function can be used to evaluate strings that
-- contain valid Oracle implemented mathematical expressions as 
-- shown below 

-- [String 1] => 1 X Random_Value + 7/16

-- [String 2] => 1 + 23 - Random_value

-- The string can have any Valid Oracle numeric function to be 
-- evaluated correctly.

SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
 v_val   SIMPLE_INTEGER := DBMS_RANDOM.value(1,5);

FUNCTION String_Eval(i_str_exp IN VARCHAR2)
 RETURN NUMBER
AS
 o_val   NUMBER;
BEGIN
 EXECUTE IMMEDIATE 'SELECT '||i_str_exp||' FROM DUAL' 
                   INTO o_val;
 RETURN o_val;
END string_Eval;
BEGIN
 DBMS_OUTPUT.PUT_LINE(CHR(13));
 DBMS_OUTPUT.PUT_LINE(' v_val = '||v_val);
 DBMS_OUTPUT.PUT_LINE(CHR(10));

 DBMS_OUTPUT.PUT_LINE(' String_Eval(1*'||v_val||'+7/16)       => '|| 
                        string_eval('1*'||v_val||'+7/16'));

 DBMS_OUTPUT.PUT_LINE(' String_Eval(1+POWER(2,3)-'||v_val||') => '||
                        string_eval('1+POWER(2,3)-'||v_val));
END;
/

=> String Evaluate Function Output

Oracle registered trademark of Oracle Corporation.


Last Revised On: October 11th, 2015

  3101