views:

506

answers:

2

We have SQL Server 2000 database with money data type columns and we have strange problem with numbers within money columns in which we store numbers with 2 decimal places. For o long time everything was OK. But now I see that in some rows where was number 47.22 is now number 47.2178. When i select CAST(COLUMN as CHAR) result is 47.22 but when i retrieve value from ADO recordset i have result 47.2178. I browse all application if there is any place where it can write number with 4 decimal places and find nothing(and in application history log there are records that application writes 47.22 to database). Can it be some SQL Server problem?

edit:application is written in VB6

+2  A: 

Are you actually using the money data type or are you using a floating point type?

What happens when you use enterprise manager to select from that table? Does everything look ok?

My guess is that you are converting the data to a floating point type somewhere along the way. Probably in the ADO code.

UPDATE

Per MS: When casting money to a string type, the machine's locale comes into play. Which is why it is rounded to 2 decimal places.

You have three options.

  1. First cast the money type to an equivalent decimal then cast that result to a char
  2. Change the machines Regional Settings to default to the format you want.
  3. Don't use the money data type to begin with, just use a decimal.
Chris Lively
we use MONEY data type, enterprise manager returns 4 decimal places
Cicik
+1 for mentioning to get away from MONEY.
Frank Kalis
have you some idea how can these 4 decimal places numbers appear in DB, because from application they really can`t
Cicik
A rounding error from 47.22 to 47.2178 seems to large to be explained by floating point approximation?
Andomar
I would agree that going from 47.22 to 47.2178 is highly unlikely as a precision problem. How is the application (not sql server) treating the data type? Is it using floating point or decimal?
Chris Lively
Also, is it a calculated column? Does the application perform math operations on the data prior to storing it?
Chris Lively
in vb6 application we use Double data type, but all times this 4 decimal places number after Rounding to 2 decimal places is equal to number which is logged as number written do DB by application
Cicik
It's possible that your logging mechanism is not writing the correct value out. I would rip out the Double data type and instead use Currency which will be much more accurate.
Chris Lively
Incidentally, floating point data types should **never** be used when money is involved. Actually they should only be used if approximations (due to precision loss) are good enough.
Chris Lively
Ok i will change Data types in application to Currency, repair DB and we will see :) thanks to all for advices
Cicik
+1  A: 

Don't use Enterprise Manager to draw any conclusions on what is really stored in your tables. EM has sometimes its own opinion on how to interpret data.

Looking at your CAST(....to CHAR) the reason is explained in the documentation (look for CAST & CONVERT)...

The following table shows the values for style that can be used for converting money or smallmoney to character data.

Value Output
0 (default) No commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 4235.98.

1 Commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 3,510.92.

2 No commas every three digits to the left of the decimal point, and four digits to the right of the decimal point; for example, 4235.9819.

EDIT: Finally figured out how to use the BlockQuote feature. :-)

Frank Kalis