Hi All,
When I run the query:
select (100/50)
It give me "2"... good.
But when I run the query:
select (50/100)
I was expected it will give me 0.5... but it gives me 0 instead? Why? and How can I get the "0.5"?
Hi All,
When I run the query:
select (100/50)
It give me "2"... good.
But when I run the query:
select (50/100)
I was expected it will give me 0.5... but it gives me 0 instead? Why? and How can I get the "0.5"?
You are seeing integer division. In T-SQL, if both operands are integers, SQL Server does the math in integer arithmetic and truncates the remainder, so 50/100 becomes 0. called out data types, which is the root cause. You do not need to move the calculation into application code as suggested by ; just make at least one operand a decimal so SQL Server promotes the calculation.
These patterns avoid integer division without changing your schema:
-- Force decimal arithmetic with an explicit CAST on one side
SELECT 50 / CAST(100 AS decimal(10,2)) AS result; -- 0.5
-- When dividing columns, cast and protect against divide-by-zero
SELECT CAST(a AS decimal(18,4)) / NULLIF(b, 0) AS ratio
FROM dbo.YourTable;
-- If you need a fixed number of decimals in the final output
SELECT CAST(CAST(a AS decimal(18,6)) / NULLIF(b, 0) AS decimal(18,2)) AS ratio2
FROM dbo.YourTable; Notes:
Jump to Post— jwshepherd 0change the resulting variable to type double
Jump to Post— bops 1im not too familiar with sql but i would assume it has something to do with data types maybe? for example if it was integer it would only return a whole number such as 2 or 0 in your case, im not too sure about this in sql as i …
Jump to Post— jwshepherd 0try doing your calculations before your sql statment
Dim i As Double Dim MySql as string i = 50 / 100 Mysql="Select " & i & " FROM table;" rs.open MySQl,conn,3,3,1
change the resulting variable to type double
im not too familiar with sql but i would assume it has something to do with data types maybe? for example if it was integer it would only return a whole number such as 2 or 0 in your case, im not too sure about this in sql as i dont do it but generally that could be the problem
try doing your calculations before your sql statment
Dim i As Double
Dim MySql as string
i = 50 / 100
Mysql="Select " & i & " FROM table;"
rs.open MySQl,conn,3,3,1 or simply "select 50. / 100"
It will work fine as long as one of the numbers in division is a float or similar.
Try
select (cast(50 as float)/100) This will work with any numeric data.
oops wujtehacjusz, you may have a look at issue date;)
oops wujtehacjusz, you may have a look at issue date;)
Damn you are right... :P
the integer division is done. so the result is in integer.
try select convert(numeric(5,2), 50)/100 OR
select 50.00/100.00
the integer division is done. so the result is in integer.
try select convert(numeric(5,2), 50)/100 OR
select 50.00/100.00
Another genius...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.