suresh
suresh

Reputation:

Divide by zero error in stored procedure

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

Upvotes: 0

Views: 1819

Answers (6)

M.N
M.N

Reputation: 11078

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.

Upvotes: 1

Spoike
Spoike

Reputation: 121772

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.

Upvotes: 1

Learning
Learning

Reputation: 8185

{ 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

Upvotes: 1

Dheer
Dheer

Reputation: 4066

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

Upvotes: 1

gnud
gnud

Reputation: 78548

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

Upvotes: 0

slim
slim

Reputation: 41223

Evidently:

{ fn LENGTH(@SearchStringtext)}

... is evaluating to zero.

Upvotes: 3

Related Questions