• Bhavesh_Patel (12/15/2009)


    Why answer differs when we are using the same function on same variable?

    Because we're not actually running the function on the same variable. In the first column, we're doing the following:

    CONVERT(DECIMAL, @TestDecimal * 0.40)

    Procedurally, this is

    1. Multiply @TestDecimal by 0.40

    2. Convert the result to DECIMAL, using the defaults (precision 18, scale 0). This is where the rounding occurs.

    In column 2, we're doing

    CONVERT(DECIMAL, @TestDecimal) * 0.40

    Procedurally, this is

    1. Convert the value in @TestDecimal to DECIMAL, using the defaults (precision 18, scale 0). Since there's nothing to the right of the decimal, the value is not changed (although the data type is.)

    2. Multiply this by 0.40. This creates a resulting value with digits to the right of the decimal point. See http://technet.microsoft.com/en-us/library/ms190476.aspx for details on how SQL Server determines the precision and scale of a result of a mathematical operation on DECIMAL values.