Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

Integer math

From Wiki

Jump to: navigation, search

What do you think the following query will return in SQL Server?

  1. 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)

  1. DECLARE @Val1 INT,@val2 INT
  2.     SELECT @Val1 =3, @val2 =2
  3.      
  4.     SELECT @Val1/@Val2


Convert explicit or implicit to get the correct answer (1.50000000000000)

  1. DECLARE @Val1 INT,@val2 INT
  2.     SELECT @Val1 =3, @val2 =2
  3.      
  4.     --Implicit
  5.     SELECT @Val1/(@Val2*1.0)
  6.     --Explicit
  7.     SELECT CONVERT(DECIMAL(18,4),@Val1)/@Val2


Contributed by: --SQLDenis 03:13, 31 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Pitfalls

186 Rating: 2.6/5 (11 votes cast)