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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error: divide by Zero VB.net SQL text 1

Status
Not open for further replies.

WordTechinc

Programmer
Sep 4, 2009
38
This error occurs while SQL text thru vb.net because ProsRNCount is zero.
This is the part of cmd.commandtext.

ISNULL(cast((RespRNCount/" & _LowestSelectedCityCounter & " ) / ProsRNCount *100 as numeric(9,3)),0) as [RNPercResponse]



This is full cmd.commandtext from vb.net

Cmd.CommandText = "select ZipCode as [Zip Code],ProsPect_Count as [Prospective count], " _
& " Responder_Count as [Responder count], " _
& " cast((Responder_Count/" & _LowestSelectedCityCounter & " ) as numeric(9,2)) as [Res.cnt/Sem], " _
& " Cast((Responder_Count/" & _LowestSelectedCityCounter & " )/ ProsPect_Count *100 as numeric(9,3))as [PercResponse], " _
& " ProsDentCount,RespDentCount, ProsNonDentCount, RespNonDentCount, " _
& " ProsRNCount as [RN Prospective count], " _
& " RespRNCount as [RN Responder count], " _
& " cast((RespRNCount/" & _LowestSelectedCityCounter & " ) as numeric(9, 2)) AS [RNRes.cnt/Sem], " _
& " ISNULL(cast((RespRNCount/" & _LowestSelectedCityCounter & " ) / ProsRNCount *100 as numeric(9,3)),0) as [RNPercResponse], " _
& " ProsNonRNCount as [NonRN Prospective count], " _
& " RespNonRNCount as [NonRN Responder count], " _
& " cast((RespNonRNCount/" & _LowestSelectedCityCounter & " ) as numeric(9, 2)) AS [NonRNRes.cnt/Sem], " _
& " cast((RespNonRNCount/" & _LowestSelectedCityCounter & " ) / ProsRNCount *100 as numeric(9,3)) as [NonRNPercResponse], " _
& " RNind,NonRNind,RNNonRNind " _
& " from dbo.INR_PLDetails where PLID in (Select PLID from INR_PLMAster where " _
& " JobCode='" & JobNo.ToString() & "')"

Dadapter.SelectCommand = Cmd
Dadapter.Fill(DtabProspPlusRespo)
Cmd.CommandText = "select ClassKey from INR_PLClassKeys where JobNo='" & JobNo & "'"
Dadapter.Fill(dtClassKeys)
 
Change ISNULL(cast((RespRNCount/" & _LowestSelectedCityCounter & " ) / ProsRNCount *100 as numeric(9,3)),0) as [RNPercResponse] to something like:

Code:
[RNPercResponse] =
CASE
	WHEN ProsRNCount = 0 THEN 0
	ELSE ISNULL(cast((RespRNCount/" & _LowestSelectedCityCounter & " ) / ProsRNCount *100 as numeric(9,3)),0) 
END
 
Thank you RiverGuy.

Could you give me another advide where to insert those statement within full cmd.commendtext for SQL pass?

 
I meant to replace the line in bold (the one at the top of your question) with the code I posted. If it's easier for you, you can delete the line breaks and make it into one line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top