SQLServerCentral » SQL Server 2005 » T-SQL (SS2K5) » Decimal Truncation in divisionInstantForum 2016-2 FinalSQLServerCentralhttps://www.sqlservercentral.com/Forums/SQLServerCentralTue, 25 Jul 2017 12:36:34 GMT20Decimal Truncation in divisionhttps://www.sqlservercentral.com/Forums/FindPost870098.aspxHi
Look at these SELECT statements.
1. SELECT CAST(1.67574 AS DECIMAL(38,10)) A, CAST(10000 AS DECIMAL(38,10)) B
2. SELECT CAST(1.67574 AS DECIMAL(38,10)) / CAST(10000 AS DECIMAL(38,10)) conv_factor
When first statement is run the result is
A B
1.6757400000 10000.0000000000
But, when the second one runs, the result is
conv_factor
0.000167
Both the numbers are converted into DECIMAL(38,10) before the division, but, why did this truncation occured?
Thanks.
Dutt.Mon, 22 Feb 2010 05:36:03 GMTduttRE: Decimal Truncation in divisionhttps://www.sqlservercentral.com/Forums/FindPost870258.aspx[quote][b]wschampheleer (2/22/2010)[/b][hr]The result of
[code="sql"]SELECT CAST(1.67574 AS DECIMAL(38,10)) / CAST(10000 AS DECIMAL(38,10)) conv_factor[/code] has datatype decimal(38,6).[/quote]
True.
[quote][b]wschampheleer (2/22/2010)[/b][hr]From BOL: In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.[/quote]
Also true, but it does not explain what is going on here.
It is true that the literal value 1.67574 would be implictly a DECIMAL(6,5). But, the CAST converts that explicitly to DECIMAL(38,10).
A similar thing is true for the literal value 10000: implied DECIMAL(5,0). But, again, explicitly CAST to DECIMAL(38,10).
So, we have two DECIMAL(38,10) values used with the division operator (/). What will be the scale and precision of the result?
Well, in this case, both operands have the same base type (decimal) so we don't need to worry about type conversion.
There is a formula for the precision and scale of this division result, contained in the reference at the end of this post.
The formula applies to two expressions e1 and e2, with precision p1 and p2, and scale s1 and s2.
In our case:
p1 = 38
p2 = 38
s1 = 10
s2 = 10
The formula for the result precision is:
(p1 - s1 + s2) + max(6, s1 + p2 + 1)
= (10 - 38 + 38) + max(6, 38 + 10 + 1)
= (10) + 49
= 59
The formula for the result scale is:
max(6, s1 + p2 + 1)
= max(6, 38 + 10 + 1)
= 49
(the imaginary max function here just returns the highest of the values passed to it)
So, our result is a DECIMAL(59,49). That is a problem since SQL Server has a maximum precision of 38 (currently). Luckily, there is a rule for this scenario too:
[i]The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.[/i]
It turns out that SQL Server never reduces the scale to less than 6 (the same magic number as in the max formulas before), and that is what happens here. There is no way divide one DECIMAL(38,10) by another and retain enough precision and scale for all valid values.
SQL Server does not take account of the actual values stored in the expressions - it applies rules which produce predictable results depending only on the precision and scale of the inputs. It makes no difference therefore, that our expected result (0.000167574) would fit in a DECIMAL(10,10) - application of the rules for dividing a DECIMAL(38,10) by a DECIMAL(38,10) result in a DECIMAL(38,6) - the maximum value for precision, and the minimum value for scale.
The essential point to take away from this is to never use a greater precision or scale than you have a sensible need for. If the original query had been this:
[code="sql"]SELECT CAST(1.67574 AS DECIMAL(6,5)) / CAST(10000 AS DECIMAL(5,0)) AS conv_factor[/code]
...the result would be 0.00016757400 - a DECIMAL(12,11).
I'm not suggesting that only decimals of exactly the right size should ever be used - but consider storage size and the potential need for increased precision when arithmetic functions are performed.
Paul
[color="#0000FF"][hr]Reference: [u][url=http://msdn.microsoft.com/en-us/library/ms190476.aspx]Precision, Scale, and Length[/url][/u][/color]Mon, 22 Feb 2010 05:36:03 GMTPaul WhiteRE: Decimal Truncation in divisionhttps://www.sqlservercentral.com/Forums/FindPost870153.aspxGot it buddies.... thanks.
Should have checked BOL before posting ;-)Mon, 22 Feb 2010 02:14:07 GMTduttRE: Decimal Truncation in divisionhttps://www.sqlservercentral.com/Forums/FindPost870145.aspxOr another option
[code="sql"]SELECT 1.67574/10000.0 as conv_factor[/code]Mon, 22 Feb 2010 02:03:33 GMTDave BallantyneRE: Decimal Truncation in divisionhttps://www.sqlservercentral.com/Forums/FindPost870101.aspxThe result of
[code="sql"]SELECT CAST(1.67574 AS DECIMAL(38,10)) / CAST(10000 AS DECIMAL(38,10)) conv_factor[/code]
has datatype decimal(38,6). From BOL: In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.
To get the desired outcome, use
[code="sql"]SELECT CAST(1.67574/10000 AS DECIMAL(38,10)) conv_factor[/code]Mon, 22 Feb 2010 00:22:44 GMTwschampheleer