Oracle错误: ORA-01722 无效数字

Oracle错误: ORA-01722 无效数字

  • 1.排查错误
    • 举个栗子
  • 2.总结
    • 几个常见的转换格式的oracle函数
      • 1. 将日期型转换为字符串TO_CHAR()
      • 2. 将数字型转换为字符串to_char()
      • 3. 将字符换转换为日期to_date()
      • 4. 将字符串转换为数字to_number()

1.排查错误

不用想了,多半是你的数据格式有问题。
先排查你的数据格式,确定是不是你想要的格式。

举个栗子

某一天我需要去按照客户的身份证明文去算下客户的年龄,按照常规逻辑,直接用当年的年份去减去截取的身份证中出生年份就够了。

select to_number(substr('20191126',1,4))-to_number(SUBSTR(a.cert_no, 7, 4)) age 
from A;

注:我的数据是外部数据导入到oracle中的。
然后我开始跑数,是没有问题的,但是我需要创建临时表,创建临时表的时候就一直报错.
ORA-01722 无效数字

扎心

然后我就去原表排查cert_no那一列中所在身份证信息,后来发现,竟然有一条加密的身份证信息:

select * from A
where cert_no  like '%*****%';

弄了一个多小时,终于找到问题了,还不是技术的问题,只是别人给的外部数据,自己没有认真查看,扎心。
oracle无效数据的图片
不需要这样的数据,加一个判断

select to_number(substr('20191126',1,4))-to_number(SUBSTR(a.cert_no, 7, 4)) age 
from A
where cert_no not like '%*****%';

搞定!

2.总结

几个常见的转换格式的oracle函数

1. 将日期型转换为字符串TO_CHAR()

// yyyy-mm-dd 也可以
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss am')
from dual;

2. 将数字型转换为字符串to_char()

select to_char(123.45678,'$99999.999') 
from dual;

3. 将字符换转换为日期to_date()

select to_date ('20191126', 'yyyy-mm-dd')
from dual;

4. 将字符串转换为数字to_number()

select to_number('01') 
from dual;

你可能感兴趣的:(Oracle)