一个varchar2的字段,判断如果是字符串,就按原来显示,如果是数字则除以12,下面这么写总是出错,请问该怎么改SQL
select distinct EFF_MONTHS,nvl2(translate(EFF_MONTHS,'\\1234567890','\\'),EFF_MONTHS,EFF_MONTHS/12)  from TH_CUST_AGRMT_X

解决方案 »

  1.   

    试验了一下 并没有出错select distinct '123',
                    nvl2(translate('123', '\\1234567890', '\\'),
                         '123',
                         to_number('123') / 12)
      from dual
      

  2.   

    select EFF_MONTHS,nvl2(translate(EFF_MONTHS,'\\1234567890','\\'),EFF_MONTHS,EFF_MONTHS/12) 
    from (select distinct EFF_MONTHS from TH_CUST_AGRMT_X) a;
      

  3.   

    To:楼上的
    Oracle对表里的所有数据都会做/12的判断 所以只要有非数字的 就报错
      

  4.   

    参考例子:SQL> create table test (col1 varchar2(20));Table created.SQL> insert into test values ('1234');1 row created.SQL> insert into test values ('abcd');1 row created.SQL> insert into test values('-34') ;1 row created.SQL> commit;Commit complete.SQL> select decode(regexp_instr(col1, '^-?\d+(\.\d+)?$'), 1, to_char(to_number(col1) / 12), col1) as result from test;RESULT
    ----------------------------------------
    102.833333333333333333333333333333333333
    abcd
    -2.8333333333333333333333333333333333333
      

  5.   

    To:楼上的
    为什么我复制你的代码创建一个表,运行Select报错,ORA-00904,regexp_instr是非法标识
      

  6.   

    ORA-00904: string: invalid identifier
    Cause: The column name entered is either missing or invalid.
    Action: Enter a valid column name. A valid column name must begin with a letter, be less than or equal to 30 characters, and consist of only alphanumeric characters and the special characters $, _, and #. If it contains other characters, then it must be enclosed in double quotation s. It may not be a reserved word. 
      

  7.   

    你的是10g或11g么?10g以前,Oracle没有实现正则表达式
      

  8.   

    数据库是9i的就是执行这个出错的,ORA-00904,regexp_instr invalid identifier
    select decode(regexp_instr(col1, '^-?\d+(\.\d+)?$'), 1, to_char(to_number(col1) / 12), col1) as result from test;
      

  9.   

    自己写一个function来判断吧create or replace function string2Number(str varchar2) return number
    is
       result number;
    begin
       result := to_number(str);
       return result;
    exception
       when others
       then return null;
    end;select decode(string2Number(col1), null, col1, string2Number(col1)/12) as result from test;RESULT                                                      
    ------------------------------------------------------------
    102.833333333333333333333333333333333333                    
    abcd                                                        
    -2.8333333333333333333333333333333333333