如何可以在select 计算一个子符串计算表达式,如
select '1*2+1' from dual如何得到结果3?而不是'1*2+1' 这个字串

解决方案 »

  1.   

    写个函数就可以了,函数就是用在sql语句里的 ,就像MAX、MIN等等一样使用
      

  2.   

    --我的是10g的没问题啊 如果实在不行 你用to_char()嘛
    scott@YPCOST> select '1*2+1' from dual;'1*2+
    -----
    1*2+1
      

  3.   

    楼主自己把它写成字符串形式了,你这么写就行了:
    select 1*2+1 from dual;
      

  4.   

    -- 实在无聊的话,写个函数:eygle@SZTYORA> create or replace function cat_comp(i_str in varchar2)
      2  return number
      3  is
      4    v_number number(18,4);
      5  begin
      6    execute immediate 'select '||i_str||' from dual' into v_number;
      7    return v_number;
      8  end;
      9  /函数已创建。已用时间:  00: 00: 00.03
    eygle@SZTYORA> select cat_comp('1*2+1') from dual;CAT_COMP('1*2+1')
    -----------------
                    3已用时间:  00: 00: 00.00
      

  5.   

    -- 加个异常处理:create or replace function cat_comp(i_str in varchar2)
    return number
    is
      v_number number(18,4);
    begin
      execute immediate 'select '||i_str||' from dual' into v_number;
      return v_number;
    exception when others
    then
      return 0;
    end;
    /
      

  6.   

    如果'1*2+1' 是某个字段的值,就只能自己写函数计算了.谢谢各位,同意luoyoumou的作法.