Char to Numeric Convert Issue -- more decimal shown

Moderator: NorbertKrupa

Post Reply
DolphinLei
Newbie
Newbie
Posts: 13
Joined: Thu Jan 10, 2013 9:53 am

Char to Numeric Convert Issue -- more decimal shown

Post by DolphinLei » Wed Mar 27, 2013 8:10 am

Hi All,
In project we are doing a sum calculation, before that we should convert this kind of char data '12,32' to '12.32', and then we want to get sum of '12.32' kind of data. our logic is here:
sum(REPLACE(Column_A, ',', '.')) as SUM_COA

However, it is strange that, the sum result is like that: 847667.639999999, actually after using replace function, every of our data will be a char, which only has 2 decimal, Can some one help to tell me why there are so much decimal in the sum result.

PS, after round the result 847667.639999999, it will be changed to 847667.64, and this is the correct result we want.

Thanks!

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Char to Numeric Convert Issue -- more decimal shown

Post by JimKnicely » Wed Mar 27, 2013 12:11 pm

Hi,

The SUM aggregate function returns a returns a DOUBLE PRECISION value for a floating-point expression. Otherwise, the return value is the same as the expression data type.

I'm not seeing the issue you are. In the following example, the result only has two decimal places:

Code: Select all

dbadmin=> select * from test;
   col
---------
 12,32
 10,14
 1231,22
(3 rows)

dbadmin=> select sum(replace(col, ',', '.')) from test;
   sum
---------
 1253.68
(1 row)
Maybe you can try explicitly converting the string to a NUMERIC having only two decimal places?

Code: Select all

dbadmin=> select sum(replace(col, ',', '.')::numeric(25,2)) from test;
   sum
---------
 1253.68
(1 row)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Char to Numeric Convert Issue -- more decimal shown

Post by id10t » Wed Mar 27, 2013 6:58 pm

Hi!

:shock: :shock: :shock:
Its a issue of representation of floating points in computers - binary vs decimal representation (nothing to Vertica).

Its a golden rule:
NEVER, but NEVER dont use floating-point numbers if precise computation is required

DolphinLei
Newbie
Newbie
Posts: 13
Joined: Thu Jan 10, 2013 9:53 am

Re: Char to Numeric Convert Issue -- more decimal shown

Post by DolphinLei » Sun Mar 31, 2013 12:54 pm

Hi Knicely,
it is interesting that, in our porject,there are 10000+ records in the calculated table, when I sum part of them with: sum(REPLACE(Column_A, ',', '.')) as SUM_COA, for example 500 rows, it displays two decimal, but when sum of it for all 10000+records., it will show : 847667.639999999. I am just curious about the reason and want to know why.

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Char to Numeric Convert Issue -- more decimal shown

Post by JimKnicely » Tue Apr 02, 2013 8:47 pm

Hi,

Read the information discussed here http://docs.python.org/2/tutorial/floatingpoint.html

Maybe it'll help you understand why you should NOT be using floats :)

If I were you I'd switch to the NUMERIC data type!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
becky
Intermediate
Intermediate
Posts: 118
Joined: Sat Apr 28, 2012 11:37 am

Re: Char to Numeric Convert Issue -- more decimal shown

Post by becky » Wed Apr 03, 2013 7:28 pm

Hmm. This is a very interesting thread.

Not to add to the confusion, but can someone explain the following phenomenon?

Code: Select all

dbadmin=> CREATE TABLE t (c FLOAT);
CREATE TABLE

dbadmin=> INSERT INTO t VALUES (123456.7899999999);
 OUTPUT
--------
      1
(1 row)
When I query the table, the value is rounded:

Code: Select all

dbadmin=> SELECT * FROM t;
     c
-----------
 123456.79
(1 row)
But what's really weird to me is why this query returns no rows:

Code: Select all

dbadmin=> SELECT * FROM t WHERE c = 123456.79;
 c
---
(0 rows)
Is it because Vertica is actually storing the original value (123456.7899999999) I inserted into the table?
THANKS - BECKSTER

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Char to Numeric Convert Issue -- more decimal shown

Post by JimKnicely » Fri Apr 05, 2013 2:06 pm

#Becky - Try querying for the value you inserted. You'll find your row :lol:

Code: Select all

dbadmin=> CREATE TABLE t (c FLOAT);
CREATE TABLE
dbadmin=> INSERT INTO t VALUES (123456.7899999999);
 OUTPUT
--------
      1
(1 row)

dbadmin=> SELECT * FROM t;
     c
-----------
 123456.79
(1 row)

dbadmin=> SELECT * FROM t WHERE c = 123456.79;
 c
---
(0 rows)

dbadmin=> SELECT * FROM t WHERE c = 123456.7899999999;
     c
-----------
 123456.79
(1 row)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “New to Vertica”