How to evaluate an expression in one table field into another?

馋奶兔 提交于 2020-01-23 16:56:04

问题


I have a table, inside there is a field called x, the x field contain value of '1+2', '1+3' etc, how to get these value and calculate it and save into another field?


回答1:


If the case you have mentioned needs to be considered then I will suggest you use the following query:

select
  xmlquery('3+4'
     returning content
  ).getNumberVal()
from
dual;

If More operators are involved except division then the aforementioned query will work but if the division operator is also involved then you must have to replace "/" with " div " keyword. Something like the following:

select
  xmlquery(
  replace( '20/5', '/', ' div ')
     returning content
  ).getNumberVal()
from
dual;

Now, you can use it in your update statement or anywhere else.

update tab 
set tab.result_column_name = xmlquery(t.your_expr_column_name
         returning content
      ).getNumberVal()

update tab 
set tab.result_column_name = xmlquery(
      replace( t.your_expr_column_name, '/', ' div ')
         returning content
      ).getNumberVal()

Demo

Cheers!!




回答2:


For simple arithmetic expressions - and depending on your Oracle version - you could use xmlquery to evaluate. Note that / has special meaning in xml, the operator for division is the keyword div - so you need a replace in case you may have forward slashes in the arithmetic expression. (If you don't have any divisions, you can simplify the query by removing the call to replace.)

Here is an example - including the test data at the top, in a with clause (not part of the solution!)

with
  test_data (str) as (
    select '1 + 3'       from dual union all
    select '3 * 5 - 2'   from dual union all
    select '2/4*6'       from dual union all
    select '3 * (1 - 3)' from dual
  )
select str, xmlquery(replace(str, '/', ' div ') returning content).getNumberVal() 
            as evaluated_expression
from   test_data;


STR         EVALUATED_EXPRESSION
----------- --------------------
1 + 3                          4
3 * 5 - 2                     13
2/4*6                          3
3 * (1 - 3)                   -6



回答3:


If you only have valid PL/SQL arithmetic expressions in your formulas, then you can use EXECUTE IMMEDIATE to evaluate them.

For this, you'll need to create a function:

create or replace function eval_expression(p_expression in varchar2)
  return number is
  query  varchar2(100);
  result number;
begin
  query := 'select ' || p_expression || ' from dual';
  execute immediate query
    into result;
  return result;
end eval_expression;

Then you can use this function in UPDATE query:

update t 
--val is another field
set t.val = eval_expression(t.x)

Naturally, with EXECUTE IMMEDIATE this query won't be extremely efficient, but it'll work. Also, with dynamic queries we're going into unsafe territory, so make sure that you don't have malicious code among your formulas.

Also, see "Evaluate Expression" on Ask TOM. Tom Kyte used a slightly more civilized approach (dbms_sql package) and created a package with a single variable support.



来源:https://stackoverflow.com/questions/56536405/how-to-evaluate-an-expression-in-one-table-field-into-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!