November 9, 2008 at 2:47 pm
All,
There are nothing less than zeros in SoldYR1 or SoldYR2. Datatype is Money for both.
Thank you.
November 9, 2008 at 6:22 pm
Please run these 2 queries:
SELECT count(*) FROM < Table Name > WHERE SoldYr1 < 0 AND SoldYr2 = 0
and
SELECT CASE WHEN t.SoldYR1 > t.SoldYR2
THEN CAST(ROUND((((t.SoldYR1 - t.SoldYR2)/(t.SoldYR1 * 1)) * 100),0) AS VARCHAR)
ELSE ''
END
FROM < Table Name >
And tell us what each of them returns.
_____________
Code for TallyGenerator
November 9, 2008 at 10:11 pm
Kishore.P (11/7/2008)
use COALESCE function instead of IsNull or NullIf.
Why? NullIf is nothing like IsNull or Coalesce and the idea of code portability is a myth.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 9, 2008 at 10:46 pm
GilaMonster (11/8/2008)
rosh (11/6/2008)
Would null values cause this.See if ISNULL helps.
Null won't cause a divide by zero. Anything divided by null (or added to null or multiplied by null, ...) is null. Only a divide by zero causes a divide by zero error
Actually Gail what I meant was would the if conditions fail because of null values....But I guess that also now is irrelevant.
November 10, 2008 at 1:11 am
rosh (11/9/2008)
GilaMonster (11/8/2008)
rosh (11/6/2008)
Would null values cause this.See if ISNULL helps.
Null won't cause a divide by zero. Anything divided by null (or added to null or multiplied by null, ...) is null. Only a divide by zero causes a divide by zero error
Actually Gail what I meant was would the if conditions fail because of null values....But I guess that also now is irrelevant.
They could, but if they did, neither of the case branches would be taken as the expression would evaluate to null and null is not true
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 11, 2008 at 2:48 am
November 11, 2008 at 2:50 am
Lynn Pettis (11/7/2008)
GilaMonster (11/6/2008)
You're dividing by t.SoldYR1 in the code that works and t.SoldYR2 in the code that doesn't. Are there any rows where t.SoldYR2 is 0?Better question, are there any rows where both t.SoldYR2 and t.SoldYR1 are zero (0)?
I think the case when statement would evaluate to false as that's a greater than condition.
November 12, 2008 at 9:07 am
Hi All,
Sorry it took me longer to reply. Rounding function using "0" or "2" makes the difference. That caused the number to zero out.
Thanks.
November 14, 2008 at 8:28 am
Hello All,
Now the following statement works fine but it produces number of digits to the right of the decimal:
CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR1 * 1.0)) * 100),0) AS VARCHAR) + '%'
Since the above produces number of digits to the right of the decimal, I want to truncate it to only 2 digits to the right of the decimal. So I am trying to convert the result to decimal, but the following gives me divide by zero error:
CAST(CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR1 * 1.0)) * 100),0) AS DECIMAL(9,0)) AS VARCHAR) + '%'
Thanks.
November 14, 2008 at 8:16 pm
ramadesai108 (11/14/2008)
Hello All,Now the following statement works fine but it produces number of digits to the right of the decimal:
CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR1 * 1.0)) * 100),0) AS VARCHAR) + '%'
Since the above produces number of digits to the right of the decimal, I want to truncate it to only 2 digits to the right of the decimal. So I am trying to convert the result to decimal, but the following gives me divide by zero error:
CAST(CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR1 * 1.0)) * 100),0) AS DECIMAL(9,0)) AS VARCHAR) + '%'
Thanks.
Could you clarify how the round function zeros out any of the variables? I'm confused because the calculation is done within the round function, and I would have thought that the rounding is done after the calculation is done.
My original comments still stand. Can you have negative values for ItmRecvdYR2? If so, then you can include a negative number for ItmRecvdYR2 and 0 for the number in ItmRecvdYR1 and thus you will get a divide by zero exception.
November 18, 2008 at 12:19 pm
Hi ta.bu.shi.ya.du,
In the following:
WHEN t.ItmRecvdYR2 > t.ItmRecvdYR1 THEN
CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR2 * 1.0)) * 100),0) AS VARCHAR) + '%'
if I change the rounding to 2 as follows, then I get divide by zero:
WHEN t.ItmRecvdYR2 > t.ItmRecvdYR1 THEN
CAST(ROUND((((t.ItmRecvdYR1 - t.ItmRecvdYR2)/(t.ItmRecvdYR2 * 1.0)) * 100),2) AS VARCHAR) + '%'
There are no zeros in any of the ItmRecvdYR1 or ItmRecvdYR2.
Thanks.
November 18, 2008 at 1:02 pm
ramadesai108, you don't make any sense, because the rounding happens AFTER dividing.
You probably need to expose whole statement to let people find where is your real problem.
Also you could find which data rows cause the error by applying some WHERE conditions.
Then you'd understand better what's really going on.
_____________
Code for TallyGenerator
Viewing 12 posts - 16 through 26 (of 26 total)
You must be logged in to reply to this topic. Login to reply