divide by zero error

Posted on 2015-02-05
Last Modified: 2015-02-05
this query is returning an error: Divide by zero error encounterd how do i fix it to make the result 0

SELECT     dbo.viewDocumentCount.[Project Name], dbo.viewDocumentCount.DocumentCount / CAST(dbo.viewTotalDays.TotalDays AS Float) AS DaysOpenAvg
FROM         dbo.viewDocumentCount INNER JOIN
                      dbo.viewTotalDays ON dbo.viewDocumentCount.ID = dbo.viewTotalDays.ID
Question by:vbnetcoder
LVL 24

Accepted Solution

Phillip Burton earned 500 total points
ID: 40591294
SELECT     dbo.viewDocumentCount.[Project Name],
case CAST(dbo.viewTotalDays.TotalDays AS Float)
when 0 then 0
dbo.viewDocumentCount.DocumentCount / CAST(dbo.viewTotalDays.TotalDays AS Float)
end AS DaysOpenAvg
FROM         dbo.viewDocumentCount INNER JOIN
                      dbo.viewTotalDays ON dbo.viewDocumentCount.ID = dbo.viewTotalDays.ID

Author Closing Comment

ID: 40591350

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 43
default constraint within a function 3 37
Need help debbuging stored procedure 21 32
T SQL Update Table from another table 5 42
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Send secure, cloud-based, encrypted alerts and maintain HIPAA compliant messaging. Integrates priority & secure messaging into one application. Ensures IT, emergency respondents and healthcare professionals that their critical messages are never mis…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now