Home > Software engineering >  What is wrong with my code why i cant assign Float Real in this code in SQL
What is wrong with my code why i cant assign Float Real in this code in SQL

Time:03-03

declare
    @num float
set @num=(1/17)
select @num as result

my result is 0

CodePudding user response:

You have to use 1.0 / 17 or 1 / 17.0 because integer division is always return integer.

DECLARE @num FLOAT
SET @num = 1.0/17
SELECT @num AS result

0.058823

DECLARE @num DECIMAL(18,2)
SET @num = 1/17.0
SELECT @num AS result

0.06

CodePudding user response:

declare
    @num float
set @num=(CAST(1 AS FLOAT)/17)
select @num as result

Result

  •  Tags:  
  • sql
  • Related