Question

Why does the following return .500000 rather than .500?

SELECT 50 / CAST(100 AS decimal(3))
Was it helpful?

Solution

Because you are casting the divisor not the result. And you have not specified precision.

See this for examples:

SELECT 50 / CAST(100 AS decimal(3))
      ,50 / CAST(100 AS decimal(6,3))
      ,CAST (50 / CAST(100 AS decimal(3)) AS decimal(3))
      ,CAST (50 / CAST(100 AS decimal(3)) AS decimal(6,3))

Precision, Scale, and Length (Transact-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.

DECIMAL uses a default precision of 18 and scale of 3.

Therefore when you case to DECIMAL(3) you get a DECIMAL(3,0). Then you use it as a divisor and SQL Server follows its rules for the resultant scale.

Operation  Result precision                     Result scale
e1 / e2    p1 - s1 + s2 + max(6, s1 + p2 + 1)   max(6, s1 + p2 + 1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top