all groups > sql server (alternate) > september 2006 >
You're in the

sql server (alternate)

group:

Unexpected Casting With Sum & Coalesce


Unexpected Casting With Sum & Coalesce Don
9/28/2006 8:37:33 PM
sql server (alternate): If I have an SQL query which returns an aggregate of several decimal fields
like so:

(sum(COALESCE(myDecimal1, 0)+
sum(COALESCE(myDecimal2, 0)+
sum(COALESCE(myDecimal3, 0)) as MyTotal

I get an rounded integer in MyTotal.

However, if I do the following:

sum(COALESCE(myDecimal1, 0)+
COALESCE(myDecimal1, 0)+
COALESCE(myDecimal1, 0)) as MyTotal

I get a (proper) decimal value.


Does anyone know why the first case returns an Integer?

- Don

Re: Unexpected Casting With Sum & Coalesce Hugo Kornelis
9/29/2006 1:06:48 AM
[quoted text, click to view]

Hi Don,

Can you please post a full repro script? (That is, post CREATE TABLE
statements, INSERT statements and SELECT statements to reproduce this
behaviour in an empty database).

--
Re: Unexpected Casting With Sum & Coalesce Ed Murphy
9/29/2006 3:42:26 AM
[quoted text, click to view]

What do the following return?

sum(coalesce(myDecimal2,0)) as MyTotal2

Re: Unexpected Casting With Sum & Coalesce Don
9/29/2006 4:55:34 PM
Sorry, I'm a bit too busy to create more details for this oddity. It's part
of a larger query, so maybe that was affecting something. When I tried
doing to two different summations alone, they both returned decimal values.

It's not big deal. I was just curious as to why it behaved that way. I
must've missed something. No worries.

- Don

Re: Unexpected Casting With Sum & Coalesce Don
10/12/2006 5:16:43 PM
I discovered what I had missed. The "scale" property for some of the
decimal fields was set to 0. This caused the COALESCE() function to
truncate the fractions.

- Don


[quoted text, click to view]

AddThis Social Bookmark Button