1

I have a column named discount of type varchar2. It contains decimal numbers as well as integers. I want to convert it into number type. So I used this query

update table_name set newdiscount=to_number(discount)

I am getting the "Not a valid number" error. Here new discount is of type number.

I checked if there are any commas present, but they aren't. Of course, there are many rows containing decimal points which are showing up in the result of query test to check any non digit values

SELECT discount FROM table_name  WHERE REGEXP_LIKE(discount, '[^[:digit:]]');

How to copy the discount values in to the newdiscount column of type number?

4

2 回答 2

1

Oracle 会话中的参数可能NLS_NUMERIC_CHARACTERS与小数点不同。

你可以这样测试它:

select 
  substr(VALUE ,1,1) as decimal_separator,
  substr(VALUE ,2,1) as group_separator
from 
  nls_session_parameters 
where 
  parameter = 'NLS_NUMERIC_CHARACTERS'

您可以在更新表之前更改会话以激活正确的分隔符:

alter session set  NLS_NUMERIC_CHARACTERS = '.,' 

或每次将小数点替换为当前小数点分隔符:

update table_name 
set newdiscount = ( select to_number( replace(discount, '.', substr(value,1,1)) )
                    from nls_session_parameters 
                    where parameter = 'NLS_NUMERIC_CHARACTERS'                  
                  )

可以在此 SQLFiddle中找到一些概念证明演示。

于 2013-06-18T08:14:26.547 回答
0

您可以尝试仅将那些验证更新为数字,例如,

update table_name
set new_discount = CASE WHEN regexp_like(discount, '^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$') THEN to_number(discount) ELSE -99999 END
于 2013-06-18T07:42:29.683 回答