[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Divide by zero error encountered.

Posted on 2016-09-29
2
Medium Priority
?
74 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
[X]
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
2 Comments
 
LVL 40

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 66

Accepted Solution

by:
Jim Horn earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

649 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