Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Division by zero error

Division by zero error

Scheduled Pinned Locked Moved Database
helpdatabasequestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Unsy
    wrote on last edited by
    #1

    Hi All I'm a newbie to SQL coding, and I have a problem which I'm hoping you can help with regarding the Division by zero error. I have a calculated field in a sql statement where the results from 1 case statements is divided by another. Is there a way to catch the division by zero error and insert a '0' without the code throwing an exception? Thank you in advance.

    W 1 Reply Last reply
    0
    • U Unsy

      Hi All I'm a newbie to SQL coding, and I have a problem which I'm hoping you can help with regarding the Division by zero error. I have a calculated field in a sql statement where the results from 1 case statements is divided by another. Is there a way to catch the division by zero error and insert a '0' without the code throwing an exception? Thank you in advance.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Unsy wrote:

      Is there a way to catch the division by zero error and insert a '0' without the code throwing an exception

      Most likely, but could you post the statement. Without that it's quite impossible to say how to implement it.

      The need to optimize rises from a bad design.My articles[^]

      U 1 Reply Last reply
      0
      • W Wendelius

        Unsy wrote:

        Is there a way to catch the division by zero error and insert a '0' without the code throwing an exception

        Most likely, but could you post the statement. Without that it's quite impossible to say how to implement it.

        The need to optimize rises from a bad design.My articles[^]

        U Offline
        U Offline
        Unsy
        wrote on last edited by
        #3

        Hi Mika I dont have the exact code with me at the moment. It was similar to this...

        SELECT
        Field1
        ,Field2
        ,SUM(
        SUM(CASE WHEN Field3 IS NOT NULL OR Term - @WeeksPassed > 0 THEN 0 ELSE 1 END))
        /
        SUM(CASE WHEN Field4 IS NOT NULL OR Term - @WeeksPassed > 0 THEN 0 ELSE 1 TotalPaid END))
        )
        FROM
        Table1
        GROUP BY
        Field1
        ,Field2

        The idea is to get a total of how many Term - 20's are over 0 which were representing a loan term. Anything less than 0 would mean it has gone over term and should not be counted. This was to be divided by the sum of total paid to calculate performance. Please excuse the SQL code if it is full of errors as I am 250 miles from home and have no way of testing if it is right. :sigh: Thanks

        W 1 Reply Last reply
        0
        • U Unsy

          Hi Mika I dont have the exact code with me at the moment. It was similar to this...

          SELECT
          Field1
          ,Field2
          ,SUM(
          SUM(CASE WHEN Field3 IS NOT NULL OR Term - @WeeksPassed > 0 THEN 0 ELSE 1 END))
          /
          SUM(CASE WHEN Field4 IS NOT NULL OR Term - @WeeksPassed > 0 THEN 0 ELSE 1 TotalPaid END))
          )
          FROM
          Table1
          GROUP BY
          Field1
          ,Field2

          The idea is to get a total of how many Term - 20's are over 0 which were representing a loan term. Anything less than 0 would mean it has gone over term and should not be counted. This was to be divided by the sum of total paid to calculate performance. Please excuse the SQL code if it is full of errors as I am 250 miles from home and have no way of testing if it is right. :sigh: Thanks

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Unsy wrote:

          Please excuse the SQL code if it is full of errors as I am 250 miles from home

          No problem. And the problem was that the divisor is equal to zero in some cases. If it's zero, you wanted the result of the division to be zero. In that case you could for example use inline view for intermediate results and make the statement something like:

          SELECT
          Field1
          ,Field2
          ,SUM( CASE Divisor
          WHEN 0 THEN 0
          ELSE Amount / Divisor
          END)
          FROM (
          SELECT Field1,
          Field2,
          SUM(CASE
          WHEN Field3 IS NOT NULL OR Term - @WeeksPassed > 0
          THEN 0
          ELSE 1
          END
          ) amount,
          SUM(CASE
          WHEN Field4 IS NOT NULL OR Term - @WeeksPassed > 0
          THEN 0
          ELSE 1
          END
          ) divisor
          FROM
          Table1
          GROUP BY
          Field1
          ,Field2) alias
          GROUP BY
          Field1
          ,Field2

          The need to optimize rises from a bad design.My articles[^]

          U 1 Reply Last reply
          0
          • W Wendelius

            Unsy wrote:

            Please excuse the SQL code if it is full of errors as I am 250 miles from home

            No problem. And the problem was that the divisor is equal to zero in some cases. If it's zero, you wanted the result of the division to be zero. In that case you could for example use inline view for intermediate results and make the statement something like:

            SELECT
            Field1
            ,Field2
            ,SUM( CASE Divisor
            WHEN 0 THEN 0
            ELSE Amount / Divisor
            END)
            FROM (
            SELECT Field1,
            Field2,
            SUM(CASE
            WHEN Field3 IS NOT NULL OR Term - @WeeksPassed > 0
            THEN 0
            ELSE 1
            END
            ) amount,
            SUM(CASE
            WHEN Field4 IS NOT NULL OR Term - @WeeksPassed > 0
            THEN 0
            ELSE 1
            END
            ) divisor
            FROM
            Table1
            GROUP BY
            Field1
            ,Field2) alias
            GROUP BY
            Field1
            ,Field2

            The need to optimize rises from a bad design.My articles[^]

            U Offline
            U Offline
            Unsy
            wrote on last edited by
            #5

            Thats spot on. Thank you. :-D

            W 1 Reply Last reply
            0
            • U Unsy

              Thats spot on. Thank you. :-D

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              You're welcome :)

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups