June 22, 2004 at 4:25 pm
SELECT Field1, Field2, etc...
FROM tbl_Purchase
WHERE (Field1 / Field2) + (Other Calculations) > 0
I have a SQL statement that is similar to the above. Field2 can be zero sometimes. This is how the data is stored and I have to do comparison based on the value of Field1 divided by Field2. Is there someway to re-write this query so that it'll still work? I need some way to do something like this:
IF (Field2 > 0) THEN
(Field1 / Field2) + (Other Calculations) > 0
ELSE
0 + (Other Calculations) > 0
and use that expression in the WHERE clause. But, TSQL doesn't work with IF statements. I've tried using the CASE statement, but I can't get it to work either. Does anyone know how to do this? Thanks!
June 22, 2004 at 6:49 pm
I think the Case statement is the way to go. Something like this:
SELECT Field1, Field2, etc...
FROM tbl_Purchase
WHERE ((case field2
when 0 then 0
else field1/field2
end)
+ (other calculations)) > 0
Regards.
June 23, 2004 at 1:27 am
If you are going to use a case statement in your WHERE clause, I would put one in your SELECT as well to warn you that field2 is zero.
June 23, 2004 at 1:51 am
So, if field2 is 0, you want to replace this with a 1 to avoid the division by zero error.
Try this:
field1 / ISNULL(NULLIF(field2, 0), 1)
The idea is that if field2 = 0, then convert the zero to a NULL (that's what NULLIF does) - then use ISNULL to replace the null with a 1
/Kenneth
June 23, 2004 at 5:40 pm
Kenneth, if field1 is anything other than zero, your solution won't produce the required result: instead it will produce (field1 + (other calculations)), in the case where field2 is zero.
However, this is a devious technique that I'd not come across before – I like it
Phil
June 24, 2004 at 3:03 am
Hmm... how do you mean?
If field1 is 2 and field2 is 0, you have 2/0 which is illegal.
What you want then is 2/1 which is 2.
Agreed, this is logically the same as 'field1 + (other calcs)' - but this is exactly what we want in this case.
It works as intended allright.
What I showed was only how to avoid division by zero errors regarding field2, it is assumed that all other fields can never have a zero value.
However, it's easy to apply it on every field that may contain a zero, but for calculation purposes should then be switched to a one.
The template is the same;
ISNULL(NULLIF(myValue, 0), 1)
=;o)
/Kenneth
June 24, 2004 at 5:54 pm
Sorry to nit-pick, but just got up at 4am to watch England exit Euro 2004 on penalties, so I'm on the war-path!
Quoting from the original post, the required result in the case of field2 being zero is "0 + (Other Calculations)", not "field1 + (other calcs)".
It's going to be a long day
June 28, 2004 at 3:41 am
Ah, my bad.
Didn't read the question close enough...
In that case,
selectCASE field2 WHEN 0 THEN 0
ELSE (field1 / field2) END
+ ( other calcs )
FROM ....
should do just as required..
/Kenneth
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy