user247527
user247527

Reputation: 43

SQL Multiply Discrepancy

I stumbled across this oddity when multiplying DECIMAL numbers on SQL Server 2005/2008. Can anyone explain the effect?

DECLARE @a DECIMAL(38,20)
DECLARE @b DECIMAL(38,20)
DECLARE @c DECIMAL(38,20)

SELECT  @a=1.0,
        @b=2345.123456789012345678,
        @c=23456789012345.999999999999999999

SELECT CASE WHEN @a*@b*@c = @c*@b*@a
       THEN 'Product is the same'
       ELSE 'Product differs'
       END

Upvotes: 4

Views: 653

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300559

It's due to precision representation and rounding errors.

The problem is due to

SELECT @a*@b   --(=2345.123457)

[Please search SO for multiple examples.]

Related: Sql Server Decimal(30,10) losing last 2 decimals

Upvotes: 2

Related Questions