Good News Everybody!
A new search engine is coming soon.
As a stop gap measure, we are using Google's custom search engine service.
|
On 17-Apr-2016 17:12 -0500, CRPence wrote:
On 11-Feb-2016 15:50 -0600, Justin Taylor wrote:
I'm trying to figure out how SQL arithmetic involving decimals
is giving an integer result. I've put together a sample query
that replicates my question.
with Values as
( select cast(1.5267 as decimal(31,4)) as A
, cast(1.6 as decimal(12,4)) as B
from SYSIBM.SYSDUMMY1
)
select Values.*
, (A - B) / B * 100.00 as RESULT
from Values;
This gives: 1.5267 1.6 0
The RESULT column has a type of decimal(31,2), but the value is
zero. With a precision of 2, RESULT should actually be -4.58.
<<SNIP>> the division portion of the expression effected a loss of
all precision to the right of the decimal point [the scale], thus
effectively producing an integer result of zero [for which
multiplication by any constant is still zero].
There is a simple and oft acceptable means to prevent the loss of
precision in the intermediate calculation; i.e. set the Minimum
Divide Scale to greater than the default of zero. <<SNIP>>
I suspect that if Justin had cast his last expression as he did in
the common table expressions, he would have got the precision he
wanted - maybe that's what you refer to, Chuck. <<SNIP>>
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.