一个varchar2的字段,判断如果是字符串,就按原来显示,如果是数字则除以12,下面这么写总是出错,请问该怎么改SQL
select distinct EFF_MONTHS,nvl2(translate(EFF_MONTHS,'\\1234567890','\\'),EFF_MONTHS,EFF_MONTHS/12) from TH_CUST_AGRMT_X
select distinct EFF_MONTHS,nvl2(translate(EFF_MONTHS,'\\1234567890','\\'),EFF_MONTHS,EFF_MONTHS/12) from TH_CUST_AGRMT_X
nvl2(translate('123', '\\1234567890', '\\'),
'123',
to_number('123') / 12)
from dual
from (select distinct EFF_MONTHS from TH_CUST_AGRMT_X) a;
Oracle对表里的所有数据都会做/12的判断 所以只要有非数字的 就报错
----------------------------------------
102.833333333333333333333333333333333333
abcd
-2.8333333333333333333333333333333333333
为什么我复制你的代码创建一个表,运行Select报错,ORA-00904,regexp_instr是非法标识
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.
select decode(regexp_instr(col1, '^-?\d+(\.\d+)?$'), 1, to_char(to_number(col1) / 12), col1) as result from test;
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