Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal separator oracle

I need to replace comma with point and then i need my value to be a number. So i wrote this:

select replace('12345,6789', ',' , '.') from dual --it works fine

but then I want to convert to_number that value and I get the error:

"invalid number"

like image 701
XLD_a Avatar asked Aug 03 '16 09:08

XLD_a


People also ask

How do I change the decimal separator in Oracle?

To switch dots and commas, change the NLS_NUMERIC_CHARACTERS: create table t ( x number ); insert into t values (1234.56); alter session set NLS_NUMERIC_CHARACTERS = '.,'; select * from t; alter session set NLS_NUMERIC_CHARACTERS = ',. '; select * from t; Note this setting is in the client.

What is the correct decimal separator?

In some countries, a raised dot or dash (upper comma) may be used for grouping or decimal separator; this is particularly common in handwriting. In the United States, the full stop or period (.) was used as the standard decimal separator.

How do you change the decimal point separator?

Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes. Tip: When you want to use the system separators again, select the Use system separators check box.

What is an example of decimal separator?

Decimal point and decimal comma are also common names for the decimal separator. For example, 9.5 means nine and one half in English speaking countries, while in many European countries, the same number might be written as 9,5.


2 Answers

The to_number() function uses the session's NLS_NUMERIC_CHARACTERS setting to decide how to interpret commas and periods. If you know your string will always have a comma as decimal separator you can override that as part of the call, using the optional third argument; although that does mean you have to specify the format model:

select to_number('12345,6789', '9999999999D9999', 'NLS_NUMERIC_CHARACTERS='',.''')
from dual;

TO_NUMBER('12345,6789','9999999999D9999
---------------------------------------
                             12345.6789

You don't need a separate replace() step.

You can also change the session's setting with ALTER SESSION SET NLS_NUMERIC_CHARACTERS=',.';, but you may not be able to control the setting in every client that has to run your code.

like image 106
Alex Poole Avatar answered Oct 05 '22 05:10

Alex Poole


The decimal separator is defined in your locale. Here it looks like it is ,. So you need not to do the replacement before converting your string:

select to_number('12345.6789') from dual --should work already

Or change your locale:

alter session  set NLS_NUMERIC_CHARACTERS= '.,';
select to_number('123'||'.'||'456') from dual;
select to_number(replace('12345,6789', ',' , '.')) from dual
like image 45
J. Chomel Avatar answered Oct 05 '22 05:10

J. Chomel