Thursday, August 13, 2009

SQL: Dividing 2 Numbers Keeps Returning Zero (0)

This is way too funny....

So I am writing a SQL Stored Procedure.... hum de dum de dum......
Run it.....

WHAT!!!! The value of X/Y is 0 (put in Column Z)
X is 5000,
y is 10000

Both are decimal data types and the third field Z is also a decimal

Shouldnt that be .50

Well to make a long long long story :) short.... the 5,000 and 10,000 values were a sum in a previous step...

What I found out after a little blog research is that when you do a math function like SUM or COUNT, SQL changes the data type to INTEGER. That is why it was returning 0

Basically here is what I did

CAST(X as decimal(18,2)) / Cast(Y as decimal(18,2))

and it worked like a charm....

Thanks to this blog that saved me:




No comments:

Post a Comment