Skip to content Skip to sidebar Skip to footer

Sql Calculation Not Showing In Correct Format

I have the following SQL query that comes out at 700 declare @a decimal(10,0) declare @b decimal(10,0) set @a = 100 - 2 set @a = @a / 14 set @a = @a * 100 set @a = ((100 - 2) /

Solution 1:

Your declaration is wrong:

declare@adecimal(10,0)
declare@bdecimal(10,0)

You have specified the length, however not the number of decimals, which is done by using the second value.

Solution 2:

700 is the correct result.

Please check again!

((100 - 2) / 14) * 100 = 700

You calculated that

100 - 2 / 14 * 100 = 85,...

You want to use this sql query.

set@a = 100 - 2.0 / 14 * 100

Solution 3:

Add a .0 to the end of your last line, since if you use all integers SQL will implicitly cast the result as an int.

set@a = ((100 - 2) / 14) * 100.0

Solution 4:

change your declarations to include decimal places:

declare@adecimal(10,5)
declare@bdecimal(10,5)

set@a=100-2set@a=@a/14set@a=@a*100set@a= ((100-2) /14) *100select@a

Since your declaration is set to 10,0 then you will get no decimal places.

if you want the answer to be 85.714... then you need to change your SQL to:

declare@adecimal(10,5)

set@a=100.0-2.0set@a=@a/14.0set@a=@a*100.0set@a=100.0- ((2.0/14.0) *100.0)

select@a

To get the result you are looking for you need to add a . to the other values (2, 14, etc) and you will get the value you want, you also need to make sure that your parentheses are in the correct location.

Results - 85.71430

Post a Comment for "Sql Calculation Not Showing In Correct Format"