ORA-01722: 无效数字 01722.00000 - "invalid number" *原因:指定的数字无效。 *行动:指定一个有效的数字
ORA-01722: invalid number 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a valid number
我是 Oracle SQL 开发人员的新手,今天 运行 这个
select r.id, r.date, it.group, it.comment, it.item, it.remark, r.summary,
substr (it.remark, instr(it.remark,'ABC')+8,7 ) as label1,
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer) as label2
from it_table it
inner join sp_table sp on sp.id = substr (it.remark, instr(it.remark,'ABC')+8,7 ) and sp.label_id = cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
inner join sq_table sq on sq.id = sp.id
where it.date > '01-jan-2020' and it.remark like '%ABC%' and it.group= 'O'
order by sp.id, it.id;
它发现了错误:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
我认为问题在于第 3 行 (cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
) 中的提取,我需要使用 cast
.
将字符串转换为数字
根据doc,尝试将字符串转换为数字时出现错误,无法将字符串转换为有效数字。
所以,我尝试替换:
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
和
to_number(substr (it.remark, instr(it.remark,'-')+1,3 ))
甚至尝试了 to_char
但没有成功。但是,原始脚本似乎在沙箱数据库中运行良好。我想知道为什么会这样。非常感谢任何帮助。
更新:
样本数据it
:
ID DATE NAME GROUP REMARK COMMENT ...
100 20-10-08 AABC X ACS LOCATION 1 - ABC IDD x105213-1
101 20-10-08 AxB Y MN LOCATION 8 - ABC IDD x105244-2
...
样本数据sp
:
ID DATE NAME GROUP label_id
105213 20-10-08 AABC X 1
105244 20-10-08 AxB Y 2
...
原来是因为remark
中有2个-
导致歧义,我只需要第二个
那么新问题:
如何提取值中的 last -
以与另一列中的另一个值连接?
使用 cast
和 default null on conversion error
以避免异常并调查转换失败的原因。
例子
with dt as
(select '001' remark from dual union all
select ' 2' from dual union all
select 'OMG' from dual)
select substr(remark,1,3) txt,
cast (substr(remark,1,3) as INT default null on conversion error) num
from dt;
TXT NUM
--- ----------
001 1
2 2
OMG
我是 Oracle SQL 开发人员的新手,今天 运行 这个
select r.id, r.date, it.group, it.comment, it.item, it.remark, r.summary,
substr (it.remark, instr(it.remark,'ABC')+8,7 ) as label1,
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer) as label2
from it_table it
inner join sp_table sp on sp.id = substr (it.remark, instr(it.remark,'ABC')+8,7 ) and sp.label_id = cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
inner join sq_table sq on sq.id = sp.id
where it.date > '01-jan-2020' and it.remark like '%ABC%' and it.group= 'O'
order by sp.id, it.id;
它发现了错误:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
我认为问题在于第 3 行 (cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
) 中的提取,我需要使用 cast
.
根据doc,尝试将字符串转换为数字时出现错误,无法将字符串转换为有效数字。
所以,我尝试替换:
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
和
to_number(substr (it.remark, instr(it.remark,'-')+1,3 ))
甚至尝试了 to_char
但没有成功。但是,原始脚本似乎在沙箱数据库中运行良好。我想知道为什么会这样。非常感谢任何帮助。
更新:
样本数据it
:
ID DATE NAME GROUP REMARK COMMENT ...
100 20-10-08 AABC X ACS LOCATION 1 - ABC IDD x105213-1
101 20-10-08 AxB Y MN LOCATION 8 - ABC IDD x105244-2
...
样本数据sp
:
ID DATE NAME GROUP label_id
105213 20-10-08 AABC X 1
105244 20-10-08 AxB Y 2
...
原来是因为remark
中有2个-
导致歧义,我只需要第二个
那么新问题:
如何提取值中的 last -
以与另一列中的另一个值连接?
使用 cast
和 default null on conversion error
以避免异常并调查转换失败的原因。
例子
with dt as
(select '001' remark from dual union all
select ' 2' from dual union all
select 'OMG' from dual)
select substr(remark,1,3) txt,
cast (substr(remark,1,3) as INT default null on conversion error) num
from dt;
TXT NUM
--- ----------
001 1
2 2
OMG