Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if then statement help

Status
Not open for further replies.

awholtsIT

IS-IT--Management
Aug 18, 2008
27
US
SQL 2005

I'm trying to eliminate a division by zero issue.
Can someone assist with proper syntax for a statement like;

if sum(Field 1)[denominator] = 0
then 0
else sum(field 2)[numerator]/sum(field 1) [denominator]

Fields 1 and 2 also need to be summed.

I keep getting an invalid expression on my line where the if function is stated.

Apreciate any help.

Thanks,

Andrew
 
Code:
SELECT CASE WHEN SUM(field 1) = 0
                 THEN 0
            ELSE sum(field 2)/sum(field 1)
       END

IF can not be used in SELECT statement.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks
That helped.

Thank you very much again,

Andrew
 
I also prefer using NULLIF technique.

In other words,

select sum(Field1)/NULLIF(Sum(Field2,0))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top