Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Please help in Divide by zero error encountered.

Author  Topic 

muzaffar_ali99
Starting Member

33 Posts

Posted - 2006-04-14 : 05:19:33
I have a problem that i am calculating percentage dividing by two fields a,b
Select a/b*100 from tbl
But the problem i am facing is if b=0 my stored procedure gives error
"Divide by zero error encountered."
I want is if b=0 replace it with 1 using some function.

Regards

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-04-14 : 05:31:07
here are 2 different ways of doing it:

declare @b int; set @b=0
select coalesce(nullif(@b,0),1)
select case when @b=0 then 1 else @b end

rockmoose
Go to Top of Page

muzaffar_ali99
Starting Member

33 Posts

Posted - 2006-04-14 : 05:54:35
Thanx It is working nicely.
Go to Top of Page
   

- Advertisement -