IT Questions and Answers :)

Friday, October 19, 2018

In Microsoft SQL Server, why does SELECT 1/2 return 0?

In Microsoft SQL Server, why does SELECT 1/2 return 0?

  • Numerator and denominator are both integers, so the result will be an integer
  • It doesn't - it returns 0.5
  • A bug in the code performing the division
  • A bug in the display in SSMS 

 
In Microsoft SQL Server, why does SELECT 1/2 return 0?

EXPLANATION

Due to the data type of the numerator and denominator being an "int", the return type is also assigned as an integer. While internally, the engine performs the calculation as 0.5, because the data type is an integer, the value gets converted to 0 and is returned as such.
Another way to say this is that the mathematical answer is 0.5 but because the system is only able to return an integer (whole number) the answer becomes 0 (the "whole number" in the answer).

Share:

0 comments:

Post a Comment

Popular Posts