Division by zero error in a stored procedure
Hi I have implemented the following stored procedure in a .net web application. Then run the application. I got this error
Division by zero error
Stored procedure:
CREATE procedure Details(@Stringtext varchar(8000),@SearchStringtext varchar(100))
as begin
SELECT ({fn LENGTH(@Stringtext)}-
{fn LENGTH({fn REPLACE(@Stringtext, @SearchStringtext, '')})})/
{ fn LENGTH(@SearchStringtext)}AS String_Count
end
0
suresh
source
to share
6 answers
If SearchStringtext is empty, its length is 0. So the stored procedure tries to divide by zero (which is undefined, which must be done ).
In other words, the next part becomes zero:
{ fn LENGTH(@SearchStringtext)}
You might want to add some logic (if possible operator) to prevent division if SearchStringtext is empty.
+1
source to share