In sql server when I do select 1 / 2 it returns 0 in stead of 0.5
Why is that?
Should not all divisions return a decimal value? Is there a setting I can set to make it divide normal?
I noticed the same in c#
What is the logic behind this?
In sql server when I do select 1 / 2 it returns 0 in stead of 0.5
Why is that?
Should not all divisions return a decimal value? Is there a setting I can set to make it divide normal?
I noticed the same in c#
What is the logic behind this?
Integer division
select 1 / 2
-- 0
Float division (at least one argument have to be float/decimal):
select 1 / 2.0
-- 0.5
select 1.0 / 2
-- 0.5
select 1.0 / 2.0
-- 0.5
If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.
EDIT:
The point is you ask why? Becasue creator of language decided so, history, convention whatsoever.
I suggest read Is integer division uniquely defined in mathematics?.
Keep in mind that in some languages you have 2 division operators (one for integer division and one for real division).
Dividing integers in a computer program requires special care. Some programming languages, such as C, treat integer division as in case 5 above, so the answer is an integer. Other languages, such as MATLAB and every computer algebra system return a rational number as the answer, as in case 3 above. These languages also provide functions to get the results of the other cases, either directly or from the result of case 3.
Names and symbols used for integer division include div, /, \, and %. Definitions vary regarding integer division when the dividend or the divisor is negative: rounding may be toward zero (so called T-division) or toward −∞ (F-division); rarer styles can occur – see Modulo operation for the details.