Oracle-ORA-01722 invalid number错误
开发人员说系统报错:ORA-01722 invalid number1. 代码里面执行了如下SQL语句:
1: select * from gl_code_combinations gcc where gcc.segment4=41030000;gl_code_combinations表里面的segment4字段是varchar2()类型字段,由于Oracle的字段类型隐式转换功能上面的SQL语句通常是可行的,查询的时候oracle解析该条件时,会首先to_char(mdn) 再和where segment4=41030000进行比较判断,如果能够确保segment4字段里面存放的全为数字那么这个语句是一直不会报错的(可能存在效率问 题),但偏偏系统设置了预算,在segment4里面加入了几个字母BUDGET,如果还是上面的语句, 当查询扫描到41030000时to_char(mdn)报错ORA-01722 invalid number2. 因此写SQL语句的时候最好还是规规矩矩的写:
select * from gl_code_combinations gcc where gcc.segment4='41030000';
什么问题都没有;
3.问题到这里没有结束,怎么把gl_code_combinations表里面的segment4字段非数字的记录找处理呢?其实方法很多,这里推荐一个非常简单的方法:
select * from gl_code_combinations gcc where UPPER(gcc.segment4) != LOWER(gcc.segment4)