Divide by zero error encountered.

Posted on 2016-09-29
Medium Priority
Last Modified: 2016-09-29

When I execute the following query, the error Divide by zero error encountered. is generated :
SELECT Object_name(qt.objectid),
       qs.execution_count                                                 AS
       [Execution Count],
       qs.execution_count / Datediff(second, qs.creation_time, Getdate()) AS
       qs.total_worker_time / qs.execution_count                          AS
       qs.total_worker_time                                               AS
       qs.total_elapsed_time / qs.execution_count                         AS
       Datediff(minute, qs.creation_time, Getdate())                      AS
       [Age in Cache]
FROM   sys.dm_exec_query_stats AS qs
       CROSS apply sys.Dm_exec_sql_text(qs.[sql_handle]) AS qt
WHERE  qt.[dbid] = Db_id()
ORDER  BY qs.execution_count DESC
OPTION (recompile);


Question by:bibi92
LVL 40

Expert Comment

ID: 41822285
Please check all divisions and make sure none of those columns or record set listed below can return NULL. Add ISNULL() around them or COALESCE()
 / Datediff(second, qs.creation_time, Getdate()) AS  [Calls/Second],
/ qs.execution_count                          AS ...
/ qs.execution_count                         AS [AvgElapsedTime],
LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 41822307
Because the denominator in a division operation is a zero.  Why else?

The usual T-SQL way of handling divide by zero errors goes like this..
CASE WHEN denominator = 0 THEN 0 ELSE numerator / denominator END as column_name

Open in new window

So take the above template and apply it to the three places you have division operators in your query.

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this article, we will see two different methods to recover deleted data. The first option will be using the transaction log to identify the operation and restore it in a specified section of the transaction log. The second option is simpler and c…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

600 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