Using NULLIF to divide by zero












2















This is the error I'm receiving below:



Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.


Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.



select  
min(round(OnCallTime*100/TotalTime,1)) as total


I'm using SQL Management Studio 2012










share|improve this question





























    2















    This is the error I'm receiving below:



    Divide by zero error encountered.
    Warning: Null value is eliminated by an aggregate or other SET operation.


    Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.



    select  
    min(round(OnCallTime*100/TotalTime,1)) as total


    I'm using SQL Management Studio 2012










    share|improve this question



























      2












      2








      2








      This is the error I'm receiving below:



      Divide by zero error encountered.
      Warning: Null value is eliminated by an aggregate or other SET operation.


      Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.



      select  
      min(round(OnCallTime*100/TotalTime,1)) as total


      I'm using SQL Management Studio 2012










      share|improve this question
















      This is the error I'm receiving below:



      Divide by zero error encountered.
      Warning: Null value is eliminated by an aggregate or other SET operation.


      Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.



      select  
      min(round(OnCallTime*100/TotalTime,1)) as total


      I'm using SQL Management Studio 2012







      sql sql-server tsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 9:37









      Mayank Porwal

      4,8952724




      4,8952724










      asked Nov 23 '18 at 9:16









      Clem_FandangoClem_Fandango

      509




      509
























          4 Answers
          4






          active

          oldest

          votes


















          3














          Use NULLIF in denominator like below:



          select  
          min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total


          So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.






          share|improve this answer
























          • Excellent, thanks Mayank

            – Clem_Fandango
            Nov 23 '18 at 9:25











          • @Clem_Fandango Please accept and upvote the answer since it worked.

            – Mayank Porwal
            Nov 23 '18 at 9:26





















          0














          use coalesce() function



          select  
          min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total





          share|improve this answer
























          • Divide by zero error means denominator is zero. In this case it won't work.

            – saravanatn
            Nov 23 '18 at 9:21











          • what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

            – fa06
            Nov 23 '18 at 9:22











          • if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

            – saravanatn
            Nov 23 '18 at 9:39



















          0














          So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.



          You can write the query as follows



          select  
          min(round(
          (OnCallTime*100)
          / (case when TotalTime =0 then null else TotalTime end)
          ,1
          )
          ) as total





          share|improve this answer































            0














            use coalesce() which return 1st non null value



            select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total





            share|improve this answer


























            • Divide by zero error means denominator is zero. In this case it won't work.

              – saravanatn
              Nov 23 '18 at 9:21











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443677%2fusing-nullif-to-divide-by-zero%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Use NULLIF in denominator like below:



            select  
            min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total


            So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.






            share|improve this answer
























            • Excellent, thanks Mayank

              – Clem_Fandango
              Nov 23 '18 at 9:25











            • @Clem_Fandango Please accept and upvote the answer since it worked.

              – Mayank Porwal
              Nov 23 '18 at 9:26


















            3














            Use NULLIF in denominator like below:



            select  
            min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total


            So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.






            share|improve this answer
























            • Excellent, thanks Mayank

              – Clem_Fandango
              Nov 23 '18 at 9:25











            • @Clem_Fandango Please accept and upvote the answer since it worked.

              – Mayank Porwal
              Nov 23 '18 at 9:26
















            3












            3








            3







            Use NULLIF in denominator like below:



            select  
            min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total


            So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.






            share|improve this answer













            Use NULLIF in denominator like below:



            select  
            min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total


            So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 9:18









            Mayank PorwalMayank Porwal

            4,8952724




            4,8952724













            • Excellent, thanks Mayank

              – Clem_Fandango
              Nov 23 '18 at 9:25











            • @Clem_Fandango Please accept and upvote the answer since it worked.

              – Mayank Porwal
              Nov 23 '18 at 9:26





















            • Excellent, thanks Mayank

              – Clem_Fandango
              Nov 23 '18 at 9:25











            • @Clem_Fandango Please accept and upvote the answer since it worked.

              – Mayank Porwal
              Nov 23 '18 at 9:26



















            Excellent, thanks Mayank

            – Clem_Fandango
            Nov 23 '18 at 9:25





            Excellent, thanks Mayank

            – Clem_Fandango
            Nov 23 '18 at 9:25













            @Clem_Fandango Please accept and upvote the answer since it worked.

            – Mayank Porwal
            Nov 23 '18 at 9:26







            @Clem_Fandango Please accept and upvote the answer since it worked.

            – Mayank Porwal
            Nov 23 '18 at 9:26















            0














            use coalesce() function



            select  
            min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total





            share|improve this answer
























            • Divide by zero error means denominator is zero. In this case it won't work.

              – saravanatn
              Nov 23 '18 at 9:21











            • what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

              – fa06
              Nov 23 '18 at 9:22











            • if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

              – saravanatn
              Nov 23 '18 at 9:39
















            0














            use coalesce() function



            select  
            min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total





            share|improve this answer
























            • Divide by zero error means denominator is zero. In this case it won't work.

              – saravanatn
              Nov 23 '18 at 9:21











            • what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

              – fa06
              Nov 23 '18 at 9:22











            • if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

              – saravanatn
              Nov 23 '18 at 9:39














            0












            0








            0







            use coalesce() function



            select  
            min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total





            share|improve this answer













            use coalesce() function



            select  
            min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 9:17









            fa06fa06

            12.7k2917




            12.7k2917













            • Divide by zero error means denominator is zero. In this case it won't work.

              – saravanatn
              Nov 23 '18 at 9:21











            • what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

              – fa06
              Nov 23 '18 at 9:22











            • if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

              – saravanatn
              Nov 23 '18 at 9:39



















            • Divide by zero error means denominator is zero. In this case it won't work.

              – saravanatn
              Nov 23 '18 at 9:21











            • what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

              – fa06
              Nov 23 '18 at 9:22











            • if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

              – saravanatn
              Nov 23 '18 at 9:39

















            Divide by zero error means denominator is zero. In this case it won't work.

            – saravanatn
            Nov 23 '18 at 9:21





            Divide by zero error means denominator is zero. In this case it won't work.

            – saravanatn
            Nov 23 '18 at 9:21













            what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

            – fa06
            Nov 23 '18 at 9:22





            what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn

            – fa06
            Nov 23 '18 at 9:22













            if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

            – saravanatn
            Nov 23 '18 at 9:39





            if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?

            – saravanatn
            Nov 23 '18 at 9:39











            0














            So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.



            You can write the query as follows



            select  
            min(round(
            (OnCallTime*100)
            / (case when TotalTime =0 then null else TotalTime end)
            ,1
            )
            ) as total





            share|improve this answer




























              0














              So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.



              You can write the query as follows



              select  
              min(round(
              (OnCallTime*100)
              / (case when TotalTime =0 then null else TotalTime end)
              ,1
              )
              ) as total





              share|improve this answer


























                0












                0








                0







                So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.



                You can write the query as follows



                select  
                min(round(
                (OnCallTime*100)
                / (case when TotalTime =0 then null else TotalTime end)
                ,1
                )
                ) as total





                share|improve this answer













                So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.



                You can write the query as follows



                select  
                min(round(
                (OnCallTime*100)
                / (case when TotalTime =0 then null else TotalTime end)
                ,1
                )
                ) as total






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 9:24









                George JosephGeorge Joseph

                1,47559




                1,47559























                    0














                    use coalesce() which return 1st non null value



                    select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total





                    share|improve this answer


























                    • Divide by zero error means denominator is zero. In this case it won't work.

                      – saravanatn
                      Nov 23 '18 at 9:21
















                    0














                    use coalesce() which return 1st non null value



                    select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total





                    share|improve this answer


























                    • Divide by zero error means denominator is zero. In this case it won't work.

                      – saravanatn
                      Nov 23 '18 at 9:21














                    0












                    0








                    0







                    use coalesce() which return 1st non null value



                    select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total





                    share|improve this answer















                    use coalesce() which return 1st non null value



                    select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 23 '18 at 9:24

























                    answered Nov 23 '18 at 9:18









                    Zaynul Abadin TuhinZaynul Abadin Tuhin

                    12.7k2932




                    12.7k2932













                    • Divide by zero error means denominator is zero. In this case it won't work.

                      – saravanatn
                      Nov 23 '18 at 9:21



















                    • Divide by zero error means denominator is zero. In this case it won't work.

                      – saravanatn
                      Nov 23 '18 at 9:21

















                    Divide by zero error means denominator is zero. In this case it won't work.

                    – saravanatn
                    Nov 23 '18 at 9:21





                    Divide by zero error means denominator is zero. In this case it won't work.

                    – saravanatn
                    Nov 23 '18 at 9:21


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443677%2fusing-nullif-to-divide-by-zero%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python