to_number() with format pattern and nvarchar2
782507Jul 1 2010 — edited Jul 1 2010Hi,
I've had a problem with converting a number in US format in to a number. Due to the settings of our Oracle we have German locale settings. Thus I use the NLS format parameters in the to_number function. It works pretty well if I set the number string directly into the function call. However it did not work a the column of type nvarchar2 (which is actually permitted according to the documentation). This simple statement shows the problem:
alter session set NLS_NUMERIC_CHARACTERS = ',.';
DECLARE
evalue NVARCHAR2(20);
evalue2 NUMBER(25, 15);
BEGIN
SELECT '5.124' INTO evalue FROM dual;
SELECT to_number(evalue,
'9G990D9999999999999999999',
'NLS_NUMERIC_CHARACTERS = ''.,''') s
INTO evalue2
FROM dual;
dbms_output.put_line(evalue2);
END;
It gives me an ORA-01722 invalid number. It can be fixed by changing the column to e.g. VARCHAR(20) or just use an cast before passing to the to_number()
DECLARE
evalue NVARCHAR2(20);
evalue2 NUMBER(25, 15);
BEGIN
SELECT '5.1234' INTO evalue FROM dual;
SELECT to_number(cast(evalue as varchar2(20)),
'9G990D9999999999999999999',
'NLS_NUMERIC_CHARACTERS = ''.,''') s
INTO evalue2
FROM dual;
dbms_output.put_line(evalue2);
END;
I played around with the number and found out that the specified NLS_NUMERIC_CHARACTERS is not applied when the column type is NVARCHAR2 and is still trying to convert it using the session's locale settings.
Does anybody know if this is fixed already in a later version?
System: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit
Kind regards,
Mario