【问题标题】:ORA-01722: invalid number 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a valid numberORA-01722: 无效号码 01722. 00000 - “无效号码” *原因:指定的号码无效。 *行动:指定一个有效的数字
【发布时间】:2022-01-12 13:14:14
【问题描述】:

我是 Oracle SQL Developer 的新手,今天在运行它时

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 - 以与另一列中的另一个值连接?

【问题讨论】:

  • 您是否验证了强制转换是问题所在 - 如果您删除该列表达式(以及匹配的连接条件),错误会消失吗? sp.id = substr(...) 在我看来也很可疑,如果 id 是一个数字列,因为您似乎将多个数据位打包到那个 remark 值中。您能否包含一些示例数据 - 特别是 remark 的示例值?
  • 嗨@AlexPoole,你是对的,事实证明错误是由remark 中有2 个- 引起的,这会导致歧义,我只需要第二个。我在问题中添加了一个示例示例。

标签: oracle substring


【解决方案1】:

使用castdefault 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           

【讨论】:

  • 您好,谢谢您的回答。事实证明,错误是因为remark中有2个-导致歧义,我只需要第二个..但是谢谢你的帮助:)
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 2023-03-15
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
相关资源
最近更新 更多