Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Integer math
From Wiki
What do you think the following query will return in SQL Server?
- SELECT 3/2
If you said 1.5 then you are wrong! The correct answer is 1, this is because when doing division with 2 integers the result will also be an integer.
There are two things you can do
1 multiply one of the integers by 1.0
2 convert one of the integers to a decimal
Integer math is integer result (1)
- DECLARE @Val1 INT,@val2 INT
- SELECT @Val1 =3, @val2 =2
- SELECT @Val1/@Val2
Convert explicit or implicit to get the correct answer (1.50000000000000)
- DECLARE @Val1 INT,@val2 INT
- SELECT @Val1 =3, @val2 =2
- --Implicit
- SELECT @Val1/(@Val2*1.0)
- --Explicit
- SELECT CONVERT(DECIMAL(18,4),@Val1)/@Val2
Contributed by: --SQLDenis 03:13, 31 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section Pitfalls



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.