Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Divide by zero error encountered.

Posted on 2016-09-29
2
42 Views
Last Modified: 2016-09-29
Hello,

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
       [Calls/Second],
       qs.total_worker_time / qs.execution_count                          AS
       [AvgWorkerTime],
       qs.total_worker_time                                               AS
       [TotalWorkerTime],
       qs.total_elapsed_time / qs.execution_count                         AS
       [AvgElapsedTime],
       qs.max_logical_reads,
       qs.max_logical_writes,
       qs.total_physical_reads,
       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);

Why?

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 39

Expert Comment

by:lcohan
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],
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41822307
>Why?
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.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 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