问题
I have value A
of type DECIMAL(19,8) - the scale is 8
, so the number of decimal digits that will be stored to the right of the decimal point is 8
.
Now, I am dividing A
on B
, where B
is BIGINT
. For, example:
SELECT CAST(3 AS DECIMAL(19, 8)) / CAST(27 AS BIGINT) -- 0.111111111111111111111111111
,CAST(300 AS DECIMAL(19, 8)) / CAST(27 AS BIGINT) -- 11.111111111111111111111111111
,CAST(75003 AS DECIMAL(19, 8)) / CAST(13664400 AS BIGINT) -- 0.005488934750153684025643277
the output values are with length: 29
, 30
, 29
respectively.
Could anyone tell why the length of the value for the three divisions is not 30
? How the SQL Server
is calculating the scale of the final result?
回答1:
Argument 1: 3 AS DECIMAL(19, 8)
Argument 2: 27 AS DECIMAL (18, 0)
-- default precision is 18, default scale is 0 (BIGINT
was converted to DECIMAL
due to type precedence)
p1 = 19
p2 = 18
s1 = 8
s2 = 0
max precision = (p1 - s1 + s2) + MAX(6, s1 + p2 + 1) -- up to 38
max scale = MAX(6, s1 + p2 + 1)
Let's calculate for example 1:
precision: (19 - 8 + 0) + MAX(6, 8 + 18 + 1) = 38
scale: MAX(6, 8 + 18 + 1) = 27
For all your examples you will get always max 27 scale.
0.111111111111111111111111111 (27)
11.111111111111111111111111111 (27)
0.005488934750153684025643277 (27)
The whole part takes only necessary digits (1), (2), (1).
For me everything is perfectly valid.
This answer is based on work of @Paul White from Decimal Truncation In division.
回答2:
This is call Data Type Precedence.
When a query do something between different but yet compatible types, one of them has to be casted to the other type, eitheir with an explicit or implicit conversion.
If you look at Data Type Conversion (Database Engine) , you will see that there is an implicit conversion between Decimal and Bigint.
Therefore you query does not requiere an explicit cast.
If you look at Data Type Precedence (Transact-SQL) on MSDN, you will see:
- decimal
- bigint
This means that decimal has a higher precedence than bigint and the bigint value will converted to decimal.
In the end, you calculation will be:
- 3,000... / 27,000...
- 300,000... / 27,000...
- 75003,000... / 27,000...
If you want it to be 3 / 27
, you must do an explicit cast on the Decimal value.
来源:https://stackoverflow.com/questions/33417022/how-scale-is-defined-when-decimal-and-bigint-are-divided