Please refer https://msdn.microsoft.com/en-IN/library/ms190476.aspx
Regarding Scale and Precision of a normal Division operation is as follows
Operation : e1 / e2
Result precision(Pr): p1 - s1 + s2 + max(6, s1 + p2 + 1)
Result scale(Sr): max(6, s1 + p2 + 1)
Where
p1 is precision of e1
s1 is scale of e1
p2 is precision of e1
s2 is scale of e1
In your case p1=p2=18 and s1=s2=2
As per the above calculation
Precision of output (Pr) and Scale of outpur(Sr) will be as follows
Pr = p1 - s1 + s2 + max(6, s1 + p2 + 1)
Pr = 18 - 2 + 2 + max(6, 2 + 18 + 1)
Pr = 18 - 2 + 2 + 21
Pr = 39
Hence there has to be maximum 39 digits in the result. But
- 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.
hence there will be 38 digits.
As division result in decimal value less than one but greater than 0 it will give only one 0 before decimal point
Now Let calculate Scale (Sr)
Sr = max(6, s1 + p2 + 1)
Sr = max(6, 2 + 18 + 1)
Sr = 21
There has to be 21 digits after decimal point.
however as per maximum Precision value last digit in scale will be truncated. that's why in you result you have got only 20 digits after decimal point.
Hence the result 0.25000000000000000000
If you want to get 0.25 the round the division using CAST or Convert as follows
DECLARE @i DECIMAL(18,3) = 2 ,@j DECIMAL(18,3) = 8
SELECT CAST((@i/@j) AS Decimal(18,2))