Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Decimal Division Accuracy

Tags:

tsql

precision

People also ask

How do I get decimal precision in SQL?

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. In SQL Server, the default maximum precision of numeric and decimal data types is 38.

How do I get 6 decimal places in SQL?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places.

Should I use float or decimal in SQL?

Float stores an approximate value and decimal stores an exact value. In summary, exact values like money should use decimal, and approximate values like scientific measurements should use float. When multiplying a non integer and dividing by that same number, decimals lose precision while floats do not.


For multiplication we simply add the number of decimal places in each argument together (using pen and paper) to work out output dec places.

But division just blows your head apart. I'm off to lie down now.

In SQL terms though, it's exactly as expected.

--Precision = p1 - s1 + s2 + max(6, s1 + p2 + 1)
--Scale = max(6, s1 + p2 + 1)

--Scale = 15 + 38 + 1 = 54
--Precision = 30 - 15 + 9 + 54 = 72
--Max P = 38, P & S are linked, so (72,54) -> (38,20)
--So, we have 38,20 output (but we don use 20 d.p. for this sum) = 11.74438969709659
SELECT CONVERT(DECIMAL(30,15),146804871.212533)/CONVERT(DECIMAL (38,9),12499999.9999)


--Scale = 15 + 38 + 1 = 54
--Precision = 30 - 15 + 15 + 54 = 84
--Max P = 38, P & S are linked, so (84,54) -> (38,8)
--So, we have 38,8 output = 11.74438969
SELECT CONVERT(DECIMAL(30,15),146804871.212533)/CONVERT(DECIMAL (38,15),12499999.9999)

You can do the same math if follow this rule too, if you treat each number pair as

  • 146804871.212533000000000 and 12499999.999900000
  • 146804871.212533000000000 and 12499999.999900000000000

To put it shortly, use DECIMAL(25,13) and you'll be fine with all calculations - you'll get precision right as declared: 12 digits before decimal dot, and 13 decimal digits after. Rule is: p+s must equal 38 and you will be on safe side! Why is this? Because of very bad implementation of arithmetic in SQL Server! Until they fix it, follow that rule.


I've noticed that if you cast the dividing value to float, it gives you the correct answer, i.e.:

select 49/30                   (result = 1)

would become:

select 49/cast(30 as float)    (result = 1.63333333333333)

We were puzzling over the magic transition,

P & S are linked, so:

  1. (72,54) -> (38,29)
  1. (84,54) -> (38,8)

Assuming (38,29) is a typo and should be (38,20), the following is the math:

  1. i. 72 - 38 = 34, ii. 54 - 34 = 20

  2. i. 84 - 38 = 46, ii. 54 - 46 = 8

And this is the reasoning:

i. Output precision less max precision is the digits we're going to throw away.

ii. Then output scale less what we're going to throw away gives us... remaining digits in the output scale.

Hope this helps anyone else trying to make sense of this.


Convert the expression not the arguments.

select  CONVERT(DECIMAL(38,36),146804871.212533 / 12499999.9999)

Using the following may help:

SELECT COL1 * 1.0 / COL2