SQLTeam.com | Weblogs | Forums

Division of two int values, result always 0

Dear all, sorry for complete newbie question.
I have Azure SQL database. In this database I have two columns with two int values. Result is not int but float. For example: I divide 8 by 141. I always get 0.
Can someone suggest me what I am doing wrong. I don't know to define new column as float, because I am using calculated colum. I even tried with: ALTER TABLE [dbo].[bmw] ADD consumption AS ([fuelLevel]/[range]*100) PERSISTED but still result is 0:

If you use ([fuelLevel]/[range]*100.0) it should be better:

DECLARE @a as INT =8;
DECLARE @b as INT =141;

SELECT @a/@b;

SELECT @a/CONVERT(decimal(8,2),@b);

If you want a float result, cast at least one of the original columns to float:

... ADD consumption AS (CAST[fuelLevel AS float)*100/[range]) PERSISTED

1 Like