'What is the best way to get a decimal or a float result when dividing two numbers in SQL [duplicate]
I declared a variable @avgMis as a decimal(2,2) in SQL Server. Then I tried to divide two integers, expecting a decimal value. Instead my answer is 0.00.
How do I divide @avgMis = (1/(1+2+1)) to get 0.25 in SQL Server?
Solution 1:[1]
cast as decimal
DECLARE @x INT = 10, @y INT = 3, @z decimal(6,2)
SELECT @z = CAST(@x AS decimal(8,4))/CAST(@y AS DECIMAL(8,4))
SELECT @z
result would be 3.33
in your situation
SELECT CAST( CAST(1 AS DECIMAL(6,2))/CAST((1+2+1) AS DECIMAL(6,2)) AS DECIMAL(6,2)) AS result
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Power Mouse |