user2135970 user2135970 - 6 months ago 11
SQL Question

math expression returns zero

Sorry to ask a dumb question, but this one's got me stumped.

SELECT
81234 / 160000 * 100 AS Try1,
CAST((81234 / 160000 * 100) AS float) AS Try2


The answer is 50.77125 but both values return zero. What's the problem?

Thanks,

Answer

Try using a decimal point.

Something like

SELECT 
81234 / 160000 * 100                    AS Try1,
CAST((81234 / 160000 * 100) AS float)   AS Try2,
81234. / 160000. * 100.                    AS Try3

SQL Fiddle DEMO

From / (Divide) (Transact-SQL)

If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.

Comments