Click to See Complete Forum and Search --> : Division in T-SQL


adambom
July 31st, 2008, 07:55 AM
Hello folks,

I'm trying to do simple division in T-SQL, but the division operator (/) always returns an integer. I need it to return at least a couple decimal places. I've tried multiple things such as:

RETURN @Var1/@Var2
RETURN CAST(@Var1 AS FLOAT)/CAST(@Var2 AS FLOAT)
RETURN CAST(CAST(@Var1 AS FLOAT)/CAST(@Var2 AS FLOAT) AS FLOAT)
RETURN CAST(@Var1 + '.0' AS FLOAT)/CAST(@Var2 + '.0' AS FLOAT)

Even 1.0/2.0 will return 0!

I have no idea why SQL Server is being this difficult. Can anybody offer help?

Alsvha
August 1st, 2008, 04:40 AM
When I do a SELECT 1.0 / 2.0 in my SSMS for 2005 I get 0.50000 as result

When I do:

DECLARE @v1 FLOAT
DECLARE @v2 FLOAT
SET @v1 = 1
SET @v2 = 2
SELECT @v1 / @v2

I get 0.5 as result.

Are you sure the problem isn't elsewhere then the division - such as you returning from a function which states it returns integers or something along that line? Thus basically you get the result cast to an integer?