数据库为Oracle,在PL/SQL中操作。
某一字段数据类型为varchar,
表中的这一字段录入的数据有的是字符有的是数字,
这是要直接对这一列进行数学运算(比如+1)会直接报错,
请教,如何只对这一列中的数值进行数学运算操作而不报错呢?

解决方案 »

  1.   


    create table t1 (c1 varchar2(5));insert into t1 values ('11');
    insert into t1 values ('');
    insert into t1 values ('123');
    commit;select c1+1 c1
    from t1
         c1
    ------------------
    1 12
    2
    3 124
      

  2.   

    -- 既然只有部分数据是数字,也就是说该字段存在非数字的值,所以你应该写个函数,将判断某行该字段是否是合法的数值,如果是合法的数值,则进行相关的数值运算。-- 例如:先写个函数,用于判断某字符是否是数值(是数值,则返回1,否则返回0):
    create or replace function is_number(i_str varchar2)
    return number
    is
      v_number number(18,0);
    begin
      select to_number(i_str) into v_number from dual;
      if v_number is not null then
        return 1;
      else
        return 0;
      end if;
    EXCEPTION
      WHEN OTHERS
      THEN
        return 0;
    end;
    /-- 然后在你要执行数值运行的时候,where条件中调用该函数,例如:
    update table, set c1=c1+1
    where is_number(c1)=1;
      

  3.   

    哦 看错了 有部分字符和数字 现在只计算是数字的?create table t1 (c1 number(5),c2 varchar2(5));insert into t1 values (1,'11');
    insert into t1 values (2,'a12');
    insert into t1 values (3,'123');
    insert into t1 values (4,'sss');
    insert into t1 values (5,'02');
    commit;select c1, c2+1 c2 
    from t1
    where regexp_like(c2,'^[0-9]*$')     c1    c2
    -----------------------
    1 1 12
    2 3 124
    3 5 3
      

  4.   

    with tb as
    (select 1 id,'s' name from dual union all
    select 1 id,'123s' name from dual union all
    select 2,'123' name from dual)
    select id, case when regexp_instr(name,'[[:alpha:]]')>0 then 0 else name+0 end+id from tb
      

  5.   

    with tb as
    (select 1 id,'s' name from dual union all
    select 1 id,'123s' name from dual union all
    select 1 id,'12 3' name from dual union all
    select 1 id,'12?3' name from dual union all
    select 2,'123' name from dual)
    select  translate(name,'/0123456789','/'),case when translate(name,'/0123456789','/') is null then  to_number(name) else 0   end+id  from tb补助楼上的漏洞多谢罗哥提醒空格等其他字符等漏洞问题
      

  6.   

    判断是否数字的条件还要加上一条 not like ‘%.%.%’
      

  7.   


    with tb as
    (select 1 id,'s' name from dual union all
    select 1 id,' ' name from dual union all
    select 1 id,null name from dual union all
    select 1 id,'s.s' name from dual union all
    select 1 id,'123s' name from dual union all
    select 1 id,'12 3' name from dual union all
    select 1 id,'12?3' name from dual union all
    select 1 id,'12.3' name from dual union all
    select 1 id,'12。3' name from dual union all
    select 1 id,'12..3' name from dual union all
    select 1 id,'我.你' name from dual union all
    select 2,'123' name from dual)
    select  translate(name,'/0123456789','/'),case when translate(name,'/0123456789','/') is null or translate(name,'/0123456789','/')='.' then  to_number(name) else 0   end+id  from tb其实觉得还是直接自己定义个函数来写luoyoumou 的我的最初只想通过SQL 来解决
      

  8.   

    http://wenku.baidu.com/view/aa620421192e45361066f54c.html参考这个吧,我就不把内容粘过来了