views:

651

answers:

6

Hi i executed the following stored procedure in .net web application. Then run the application. I got this error

" Divide 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

comments are all welcome

thanks suresh

+2  A: 

Evidently:

{ fn LENGTH(@SearchStringtext)}

... is evaluating to zero.

slim
A: 

It seems that the length of SearchStringtext is 0 -- so the procedure tries to divide by zero.

gnud
+1  A: 

If SearchStringtext is empty, the length of it becomes 0. Thus the stored procedure tries to divide by zero (which is an undefined thing to do).

In other words the following part becomes zero:

{ fn LENGTH(@SearchStringtext)}

You might want to add some logic (if statement perhaps) to prevent the division to happen if the SearchStringtext is empty.

Spoike
+1  A: 

The length of the SearchStringText is Zero and hence there is divide by zero error. Make sure that when the function is called, the string is of non-zero length. Alternatively check for length before doing the select

Dheer
+1  A: 
{ fn LENGTH(@SearchStringtext)}

is evaluating to 0.

However, why is this a stored procedure? You are not using any db feature? Unless this is a simplified problem , all this (length, replace etc) could be done in your .net application

Learning
+1  A: 

The only division operation in this procedure, has fn LENGTH(@SearchStringtext) as the divisor.

Hence it seems that length of **SearchStringtext** is evaluating to zero. It might be possible that you are trying to search an Empty string.

Please check and then elaborate the question details, along with the DB platform.

Mohit Nanda