Search code examples
sqlfirebirdarithmetic-expressions

Convert string into arithmetic operators


I have an equation stored in a table and want my SQL to return the calculated value.

e.g

Select replace (c.equation, 'x', 121) 
  from config c where key= 2 

The above returns (8.5-(121-20)/100) . I need the calculated value ,i.e 7.49

I tried the following but not working :

Select cast(replace (g.equation, 'x', 121) as double precision) 
  from config c where key= 2 

Any idea please?


Solution

  • As stated by @Arioch you have to use EXECUTE STATEMENT. Than just put equation in following procedure CALCULATE

    CREATE OR ALTER PROCEDURE calculate (equation VARCHAR(100))
    RETURNS (result VARCHAR(10))
    AS
    begin
      EXECUTE STATEMENT ('SELECT ' || :EQUATION || ' FROM rdb$database')
      INTO :result;
    
      suspend;
    end