如何将一个表字段中的表达式计算为另一个? [英] How to evaluate an expression in one table field into another?

查看:57
本文介绍了如何将一个表字段中的表达式计算为另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,里面有一个叫x的字段,x字段包含'1+2'、'1+3'等值,如何获取这些值并计算并保存到另一个字段中?

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?

推荐答案

如果您提到的情况需要考虑,那么我建议您使用以下查询:

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;

如果除除法外涉及更多运算符,则上述查询将起作用,但如果还涉及除法运算符,则必须将/"替换为div"关键字.类似于以下内容:

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()

演示

干杯!!

这篇关于如何将一个表字段中的表达式计算为另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆