I am trying to get a ratio from the following statement:
Select ((query1)/(query2)) AS Ratio.
The Result is 0. the actual result is .34 So SQL is rounding to zero. How can I fix this?
I am trying to get a ratio from the following statement:
Select ((query1)/(query2)) AS Ratio.
The Result is 0. the actual result is .34 So SQL is rounding to zero. How can I fix this?
Select(
(SELECT 8) --<-- Query 1 returns INT 8
/
(SELECT 5) --<-- Query 2 returns INT 5
) AS Ratio
Because both values are INT but when divided you get a decimal value back, Sql Server does an implicit conversion to INT and truncates any Decimal point and returns a whole number back.
Above query returns 1
Select(
CAST((SELECT 8)AS NUMERIC(18,5)) --<-- Query 1 CAST as a DECIMAL or NUMERIC
/
CAST((SELECT 5) AS NUMERIC(18,5)) --<-- Query 2 CAST as a DECIMAL or NUMERIC
) AS Ratio
When converted to Numeric datatype results retuned by both queries sql server does an implicit coversion on the result returned by dividing both queries and returns
Result : 1.60000000000000000000