【问题标题】:Character to number conversion error字符到数字转换错误
【发布时间】:2012-01-31 04:53:30
【问题描述】:
declare
   l_tot number := 0;

begin
   for i in 1..apex_application.g_f08.count loop
      l_tot := l_tot + nvl(to_number(apex_application.g_f08(i)),0);
   end loop;

 if l_tot = nvl(to_number(:P21_TOTAL_PRICE),0) then
      return true;
   else
      return false;
   end if;
end;

上面的代码出现以下错误

ORA-06502: PL/SQL: numeric or value error: character to number conversion error

:P21_TOTAL_PRICE 发生错误。怎么了?我该如何纠正这个问题?

【问题讨论】:

  • 价格字段中是否有逗号或空格?
  • @Sathya 是的。 :P21_TOTAL_PRICE 中的值,例如 5,500.00

标签: oracle plsql oracle10g


【解决方案1】:

您应该使用更强大的 REGEXP_REPLACE 函数,而不是使用 REPLACE。 http://www.orafaq.com/wiki/REGEXP_REPLACE

然后,您可以在使用 TO_NUMBER 函数之前从字符串中删除任何非数字字符。

在你的情况下,它会是这样的:

REGEXP_REPLACE(:P21_TOTAL_PRICE, '[^0-9]+', ''); 

在此处查看我对几乎完全相同的问题的回答:Oracle To_Char function How to handle if it's already a string

【讨论】:

    【解决方案2】:

    错误上升是因为您所代表的数字实际上是一个包含逗号等的字符串。当您将 to_number 放入其中时,Oracle 无法替换逗号。

    您可能想使用replace 函数去除逗号

    改变

    if l_tot = nvl(to_number(:P21_TOTAL_PRICE),0) then
    

    if l_tot = nvl(to_number(replace(:P21_TOTAL_PRICE,',','')),0) then  
    

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 2023-03-21
      • 2015-02-26
      • 2012-07-15
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2013-08-24
      相关资源
      最近更新 更多