Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query output problem

Status
Not open for further replies.

tapks

IS-IT--Management
Sep 6, 2001
72
IN
Why the output of the following calculation is shown as 0 in query analyser, where as the output should be 0.207 (approx)?

select 61*4*31/36500

Can anybody help me out?

Thankx in advance
 
you have to do the calculation in two steps i think

declare @res decimal(9, 5)
set @res = (61*4*31)
set @res = @res/36500
print @res

result:0.20723

Steve
 
ahhh I think this has more to do with implicit casting
if you do the following
declare @res decimal(9,5)
set @res = (61.0 * 4.0 * 31.0)/36500.0
print @res

then you'll get the right result. i'm presuming that sql server evaluates the brackets first and then hold that as an int then does the division. as the value is an int the bits after the decimal point are lost

e.g.

declare @res real
set @res = (3+5+2)/3
print @res

returns 3
 
I believe it is the presence or absence of decimals. Calculations against integers return integers, calculations against decimals return decimals. In tapks' example, all values are integers, so the solution resolves to an integer, zero. Both of SteveBrett's example use decimals, so his results are decimals.

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top