all groups > sql server programming > june 2005 >
You're in the

sql server programming

group:

Arithmetic with integer and money data types produces invalid results


Arithmetic with integer and money data types produces invalid results Roberto Kohler
6/8/2005 9:34:59 PM
sql server programming:
Arithmetic with integer and money data types produces invalid results.
Is this a known SQL bug?
Are there any guidelines for doing arithmetic with the money data type?

The following operation
select round(@amount *((@units*@unitPrice)/@invoiceTotal),2)
returns an incorrect value
if @amount, @unitPrice and @invoiceTotal are of type money
and @units is of type int

round(3742.20 * (2 * 43.48) / (188338.77 - 24565.93), 2)
returns 1.73 which is correct
round(@amount * ((@units*@unitPrice)/@invoiceTotal), 2)
returns 1.50 which is wrong

You can try it:

declare @amount money
declare @units int
declare @unitPrice money
declare @invoiceTotal money

select @amount = 3742.20
select @units = 2
select @unitPrice = 43.48
select @invoiceTotal = 188338.77


select round(3742.20 * ((2 * 43.48) / 188338.77), 2) "correct answer",
round(@amount * ((@units*@unitPrice)/@invoiceTotal),2) "wrong
answer"


correct answer wrong answer
-------------------------- ---------------------
1.7300000000000 1.5000

Re: Arithmetic with integer and money data types produces invalid results Steve Kass
6/8/2005 11:57:47 PM
Roberto,

Microsoft calls it "by design", but I would be more specific:
"by poor design". You are right that this is a problem. The reason
is that expressions of the form <money>/<money> and
<money>*<money> should not have datatype money, but
they do. You are better off using another data type if you need
to multiply or divide monetary values.

For some examples and discussion of this, see
http://groups.google.co.uk/groups?q=D3F842EE-4910-40CA-A345

Steve Kass
Drew University

[quoted text, click to view]
AddThis Social Bookmark Button