How to evaluate and get results of formula stored in column?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Rover
    New Member
    • Feb 2011
    • 2

    How to evaluate and get results of formula stored in column?

    I've a column with a string like this
    "=1*.003*2* 3" and I want to evalute this formula
    to get the result (in the example is 0.018).
    How I can get the rusult? Thanks in advance
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Remove = with repexp_replace and use dynamic query
    Code:
    FOR i in SELECT column FROM your_table LOOP
    EXECUTE IMMEDIATE 'select '||i.column||' FROM DUAL ' INTO some_defined_variable;
    ....
    END LOOP;

    Comment

    • Marco Rover
      New Member
      • Feb 2011
      • 2

      #3
      This was what I really need:

      SQL> CREATE OR REPLACE FUNCTION calc(pi_val VARCHAR2) RETURN NUMBER IS
      2 v_return NUMBER;
      3 BEGIN
      4 EXECUTE IMMEDIATE 'select '||pi_val||' from dual' INTO v_return;
      5 RETURN v_return;
      6 END;
      7 /

      Function created
      SQL> SELECT calc('2*6*10') FROM dual;

      CALC('2*6*10')
      --------------
      120

      I found the solution at this link:
      If I have a string say "3*2+24" how can calculate its value in Oracle? In sql server you can do exec ('select 3*2+24') and it returns 30 Thanks


      Thanks anyway.

      Comment

      Working...