Click here to Skip to main content
12,753,788 members (31,548 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 7 May 2014

Losing Precision After Multiplication in SQL Server

, 7 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Losing precision after multiplication in SQL Server

Yesterday, I was doing a little multiplication operation in SQL SERVER. The computation involves multiplication of two decimal numbers. A point to note here is that I've to be very careful while performing the calculations because it's a monetary thing. So, whatever the calculation made, it must be very accurate such that I shouldn't miss any fractions as it costs in MILLIONS.

The scenario is that I've two numbers, after multiplying each other I'm losing the precision of the values. I've tried to check my database for any faulty data that caused the issue. But the data is absolutely fine.

Here is what the SQL looks like:

SET @NUM1 = 1.86975178  
SET @NUM2 = 6.65781856     
--The expected output: 12.4484681034770368000  
--The original output: 12.448468

I was surprised to see the result that I've got only has 6 decimal places. What went wrong?

To understand the problem, we need to have an idea of the concept called Precision, Scale and Length. The following table depicts that there are few rules to be followed when we are calculating the numeric values or decimal values.

Operation Result precision Result scale *
e1 + e2 max(s1, s2) + max(p1-s1, p2-s2) + 1 max(s1, s2)
e1 - e2 max(s1, s2) + max(p1-s1, p2-s2) + 1 max(s1, s2)
e1 * e2 p1 + p2 + 1 s1 + s2
e1 / e2 p1 - s1 + s2 + max(6, s1 + p2 + 1) max(6, s1 + p2 + 1)
e1 { UNION | EXCEPT | INTERSECT } e2 max(s1, s2) + max(p1-s1, p2-s2) max(s1, s2)
e1 % e2 min(p1-s1, p2 -s2) + max( s1,s2 ) max(s1, s2)

* 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.

For those who don't know what precision and scale is, precision is the total number of digits and scale is the number of decimals. Say for example, a data type decimal(10,5). Then the precision is 10 and scale is 5. So for multiplication of two decimal numbers, the result precision and the resultant scale are shown as follows:

Precision = P1 + P2 +1 = 32 + 32 + 1 = 65
Scale = S1 + S2 = 10 + 10 = 20

Theoretically, the result should be of DECIMAL(65,20), but this is INVALID as the maximum capacity of the DECIMAL is 38 only. As the * below the table states, if the result is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated. So our result precision is greater than 38, SQL server tries to avoid truncating the integral part by reducing the scale, i.e., instead of removing the integer part, SQL server will truncate the decimal part. SQL SERVER team has decided to preserve scale of 6 in both division and multiplication scenarios. So our DECIMAL(65,20) is turned into DECIMAL(38,6).

The solution is that we need to minimize the use of precision and scale in the data type that we use such that the resultant precision is not exceeding 38. Now in this case, we'll adjust the datatype to get some accuracy in the data.

DECLARE @NUM2 DECIMAL(18, 10)       
SET @NUM1 = 1.86975178   
SET @NUM2 = 6.65781856       
--Our result: 12.44846810347703680000

Now the resultant data type will be DECIMAL(37,20) which is less than the maximum of the decimal's precision limit and hence no implicit reduction is performed.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Karthik Chintala
Software Developer (Senior)
India India
Developer. Blogger.

Follow my Blog

You may also be interested in...


Comments and Discussions

GeneralThanks for this post Pin
Member 1130085822-Sep-15 5:45
memberMember 1130085822-Sep-15 5:45 
GeneralMy vote of 5 Pin
eghetto27-May-14 22:08
membereghetto27-May-14 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 7 May 2014
Article Copyright 2014 by Karthik Chintala
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid