Solved

SQl : Divide by Zero Encountered ..!!!

Posted on 2011-09-12
14
410 Views
Last Modified: 2012-05-12

SQL 2005 / 2008


SQL 2005 / 2008

I am facing Exception Error on below Syntax :-

select x.MemberName,x.Directions,x.Name,x.QtyOrdered,x.DaysSupply,x.DateFilled,
CASE      WHEN x.test = 0  THEN 'N/A'  WHEN compliance > 100.0   THEN '100.0'      
               ELSE CONVERT(VARCHAR(5),

-- CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))      END as [Compliance]
CAST(FLOOR(compliance *10) / 10.0 AS DECIMAL(6,1)))  END as [Compliance]

***

1) Error occurs only for a certain inputs, not for all.
2) Compliance column has to accept nvarchar and float. If there is no compliance, we set the value to be 'N/A'. if there is compliance it could be .. 98.9% or etc .. by making sure that they dont exceed 100%


0
Comment
Question by:chokka
[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
  • 7
  • 6
14 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36525707
Hmm. I don't actually see a division there, can you post the full query.
0
 
LVL 10

Expert Comment

by:dwe761
ID: 36525733
what are the inputs that cause the error?
0
 

Author Comment

by:chokka
ID: 36525851

-- CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))      END as [Compliance]

CAST(FLOOR(compliance *10) / 10.0 AS DECIMAL(6,1)))  END as [Compliance]


I tried casting the compliance column ..
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:chokka
ID: 36525871



Input fields are firstname,lastname and DOB ...

So for some people it works great .. it breaks down only on certain people ..!!

I have no idea, i believe the error throws mainly due to casting , ..

Technically, we can't divide a Number by Zero.
Try 5/0 or 6/0 ... or Any Number / 0 is not possible ..!!

So, on this Casting i need to write .. N/A for any number is going to be divided by Zero ..!!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36525878
You are dividing by a literal 10.0 there, so I am not sure how you can get that error. Now, you could end up with a numerator of 0 if (compliance * 10) is still less than 1 as FLOOR() will force to 0, but that should just result in 0 which should not be an issue unless you are then dividing by this later.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36525889
I understand you can't divide by 0, but my point is that you are not dividing by variables here unless you showed us a query that doesn't reflect the real-life scenario. Hence my request to post the whole thing. :)

Note you use x.test = 0 and not compliance = 0, so to get an understanding it helps if we see everything. Maybe post samples of the data that fail and the complete code and we can take a look.
0
 

Author Comment

by:chokka
ID: 36526366
Please find the complete query attached ..!!
select x.MemberName,x.DOB,x.FilePath,x.Medication,x.NDC,x.Directions,x.Name,x.Strength,x.GenericName,x.QtyOrdered,x.DaysSupply,x.DateFilled, 
CASE      WHEN x.test = 0  THEN 'N/A'  WHEN compliance > 100.0   THEN '100.0'      ELSE CONVERT(VARCHAR(7), 
-- CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))      END as [Compliance]
CAST(FLOOR(compliance *10) / 10.0 AS DECIMAL(6,1)))  END as [Compliance]


from (
SELECT cm.*, ( ( (SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
            FROM   (SELECT	FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled 
                    FROM   voeorderhistorywide 
                    UNION ALL 
                    SELECT FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled  
                    FROM   voeorderwide) AS x 
            WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
				   AND ndc = cm.ndc 
                   AND datefilled >= '03/15/2010') - 
                  (SELECT TOP 1 dayssupply 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
							AND LastName = 'Dale'
						 AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= 
                              '03/15/2010' 
                   ORDER  BY datefilled DESC) ) / 
                  Datediff(d, (SELECT TOP 1 datefilled 
                               FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderhistorywide 
                                       UNION ALL 
                                       SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderwide)AS x 
                               WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
                                      AND ndc = cm.ndc 
                                      AND datefilled >= '03/15/2010' 
                               ORDER  BY datefilled ASC), 
                  (SELECT TOP 1 datefilled 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= '03/15/2010' 
                   ORDER  BY datefilled DESC)) ) * 100.0 AS Compliance , 
         ( (SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
            FROM   (SELECT      FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled 
                    FROM   voeorderhistorywide 
                    UNION ALL 
                    SELECT FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled  
                    FROM   voeorderwide) AS x 
            WHERE  FirstName = 'Robert'
                           AND LastName = 'Dale'
                           AND DateOfBirth = '02/22/1980'
                           AND ndc = cm.ndc 
                   AND datefilled >= '03/15/2010') - 
                  (SELECT TOP 1 dayssupply 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
                                          AND LastName ='Dale'
                                     AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= 
                              '03/15/2010' 
                   ORDER  BY datefilled DESC) ) as test
FROM   [Udf_compliancemembers] ('Robert','Dale','02/22/1980' ) cm

) x

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36526461
Look in your calculation for compliance you have a division with a DATEDIFF - if you get back date times that are on the same day then your denominator will evaluate to zero; hence error message!
0
 

Author Comment

by:chokka
ID: 36529322
Yes, if i am getting this error and i need to display it as "N/A" on the Compliance field ..!!

i.e) if my denominator is zero, then before dividing i need to assign the value to the Compliance field as "N/A"
0
 

Author Comment

by:chokka
ID: 36529464
Actually i face an error on this part of the Source Code.

You can see the Test Column - Which is generated by Dvision
SELECT cm.*, ( ( (SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
            FROM   (SELECT	FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled 
                    FROM   voeorderhistorywide 
                    UNION ALL 
                    SELECT FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled  
                    FROM   voeorderwide) AS x 
            WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
				   AND ndc = cm.ndc 
                   AND datefilled >= '03/15/2010') - 
                  (SELECT TOP 1 dayssupply 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
							AND LastName = 'Dale'
						 AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= 
                              '03/15/2010' 
                   ORDER  BY datefilled DESC) ) / 
                  Datediff(d, (SELECT TOP 1 datefilled 
                               FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderhistorywide 
                                       UNION ALL 
                                       SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderwide)AS x 
                               WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
                                      AND ndc = cm.ndc 
                                      AND datefilled >= '03/15/2010' 
                               ORDER  BY datefilled ASC), 
                  (SELECT TOP 1 datefilled 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
				   AND LastName = 'Dale'
				   AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= '03/15/2010' 
                   ORDER  BY datefilled DESC)) ) * 100.0 AS Compliance , 
         ( (SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
            FROM   (SELECT      FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled 
                    FROM   voeorderhistorywide 
                    UNION ALL 
                    SELECT FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled  
                    FROM   voeorderwide) AS x 
            WHERE  FirstName = 'Robert'
                           AND LastName = 'Dale'
                           AND DateOfBirth = '02/22/1980'
                           AND ndc = cm.ndc 
                   AND datefilled >= '03/15/2010') - 
                  (SELECT TOP 1 dayssupply 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Robert'
                                          AND LastName ='Dale'
                                     AND DateOfBirth = '02/22/1980'
                          AND ndc = cm.ndc 
                          AND datefilled >= 
                              '03/15/2010' 
                   ORDER  BY datefilled DESC) ) as test
FROM   [Udf_compliancemembers] ('Robert','Dale','02/22/1980' ) cm

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36531050
That looks like you are pointing to the DATEDIFF piece of the code that I already directed you to above. Is that correct? It is very unclear what it is you are saying ... the error is before you are assigning the 'N/A' in the case statement. Try splitting up that extremely large calculation filled with subqueries. i.e., bring back each subquery to a different column then do the calculation in the outer query where you can use CASE to inspect whether or not the DATEDIFF results in 0.
0
 

Author Comment

by:chokka
ID: 36531364
@mwvisa1, Yes you are perfectly right ..!!!

But now, in that DateDiff Part of syntax - I am trying to implement Case When Statement

CASE   WHEN    (SQL QUERY) = 0  THEN 'N/A'  
               
SELECT cm.*, ( ( (	SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
FROM			(
					SELECT		FirstName,LastName,	DateOfBirth,dayssupply,ndc,datefilled 
                    FROM		voeorderhistorywide 
                    UNION ALL 
                    SELECT		FirstName,LastName,DateOfBirth,dayssupply,
								ndc,datefilled  
                    FROM		voeorderwide
				)	AS x 
					WHERE		FirstName = 'Rose' AND	LastName = 'Post' AND DateOfBirth = '10/02/1926'
					AND			ndc = cm.ndc AND datefilled >= '03/15/2010') - 
                  (	SELECT TOP 1 dayssupply 
					FROM   (	SELECT	FirstName,LastName,DateOfBirth,dayssupply,datefilled,
										ndc FROM   voeorderhistorywide 
								UNION ALL SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
									FROM   voeorderwide)AS x 
                   WHERE		FirstName = 'Rose' AND LastName = 'Post'  AND DateOfBirth = '10/02/1926'
                          AND ndc = cm.ndc AND datefilled >=  '03/15/2010' ORDER  BY datefilled DESC) ) 

					/ 

                  Datediff(d, (SELECT TOP 1 datefilled 
                               FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderhistorywide 
                                       UNION ALL 
                                       SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                                       FROM   voeorderwide)AS x 
                               WHERE  FirstName = 'Rose'
				   AND LastName = 'Post'
				   AND DateOfBirth = '10/02/1926'
                                      AND ndc = cm.ndc 
                                      AND datefilled >= '03/15/2010' 
                               ORDER  BY datefilled ASC), 
                  (SELECT TOP 1 datefilled 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Rose'
				   AND LastName = 'Post'
				   AND DateOfBirth = '10/02/1926'
                          AND ndc = cm.ndc 
                          AND datefilled >= '03/15/2010' 
                   ORDER  BY datefilled DESC)) ) * 100.0 AS Compliance , 
         ( (SELECT 1.0 * Coalesce(SUM(dayssupply), 0) AS sumdayssupply 
            FROM   (SELECT      FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled 
                    FROM   voeorderhistorywide 
                    UNION ALL 
                    SELECT FirstName,LastName,DateOfBirth,dayssupply,ndc,datefilled  
                    FROM   voeorderwide) AS x 
            WHERE  FirstName = 'Rose'
                           AND LastName = 'Post'
                           AND DateOfBirth = '10/02/1926'
                           AND ndc = cm.ndc 
                   AND datefilled >= '03/15/2010') - 
                  (SELECT TOP 1 dayssupply 
                   FROM   (SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderhistorywide 
                           UNION ALL 
                           SELECT FirstName, LastName,DateOfBirth,dayssupply,datefilled,ndc 
                           FROM   voeorderwide)AS x 
                   WHERE  FirstName = 'Rose'
                                          AND LastName ='Post'
                                     AND DateOfBirth = '10/02/1926'
                          AND ndc = cm.ndc 
                          AND datefilled >= 
                              '03/15/2010' 
                   ORDER  BY datefilled DESC) ) as test
FROM   [Udf_compliancemembers] ('Rose','Post','10/02/1926' ) cm

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36531577
That is going to be very confusing to maintain as you will have:

CASE
   WHEN DateDiff(d, ({some query}), ({another query}))  = 0
      THEN 'N/A'
   ELSE ({yet another query}) / DateDiff(d, ({some query}), ({another query}))
END

I would recommend highly, that you do this:

SELECT CASE DateDiff(d, x, y) WHEN 0 THEN 'N/A' ELSE z / DateDiff(d, x, y) END
FROM (
   SELECT ({some query}) as x, ({another query}) as y, ({yet another query}) as z
) derived

Make sense?
0
 

Author Closing Comment

by:chokka
ID: 36531704
Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

627 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