字符到数字转换错误

时间:2012-01-31 04:53:30

标签: oracle plsql oracle10g

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出错。怎么了?我怎么能纠正这个?

2 个答案:

答案 0 :(得分:4)

您应该使用功能更强大的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

答案 1 :(得分: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