Results 1 to 13 of 13

Thread: divide by 0 error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    why is this causing a divide by 0 error???

    update #totals
    set units_cancel_percent = units_sent/units_cancelled

    in my temp table I am trying to divide the results of these two colums by each other in an update statement; how can I do this????

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    units_cancelled is probably 0 then...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    i don't understand meanig it was trying to divide by 0 how can i work around this problem????

  4. #4
    New Member
    Join Date
    May 2000
    Location
    Auburn,AL
    Posts
    12
    If your units_cancelled variable could be zero, you need to use an if_then statement to check it before the computation.

    If units_cancelled = 0 then
    units_cancel_percent = 0
    else
    set units_cancel_percent = units_sent/units_cancelled

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You need to use an expression in your update statement. I know that in Access SQL (Jet), you can use VB functions (such as iif) within the SQL statement, such as this:

    update #totals
    set units_cancel_percent =
    iif(units_cancelled = 0, 0, units_sent/units_cancelled)

    If you are using SQL Server or other database, look into the allowable functions. I'm looking at a SQL Server book right now, and I see that you can do something like this:

    update #totals
    set units_cancel_percent =
    case units_cancelled
    when 0 then 0
    else units_sent/units_cancelled
    end

    I hope this was helpful.
    "It's cold gin time again ..."

    Check out my website here.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    this seems to work:

    update #totals
    set units_shipped_day1_percent = units_sent/units_shipped_day1
    where units_shipped_day1 <> 0
    and units_shipped_day1 is not null

    but how do i get the results to return a percentage??? and have the % on it??????

    Let me know if there is a better way to code this too????

    thanks

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    set units_shipped_day1_percent = 100*units_sent/units_shipped_day1 & "%"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Server: Msg 245, Level 16, State 1, Line 1
    Syntax error converting the varchar value '%' to a column of data type int

    get this error anyway around it???

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    does anyone know how to get his to be a percentage - converted to a char ? and add the % on to it?????

    THANKS!!!

  10. #10
    Addicted Member
    Join Date
    May 1999
    Posts
    161
    I don't know much about Jet Databases but why don't you store the data as a decimal, and just convert when you retrieve and want to display your data... ?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    okay so the column is a decimal how do i then convert the syntax can you give an example thanks

  12. #12
    Addicted Member
    Join Date
    May 1999
    Posts
    161
    You need to use the 'Format' function.

    For instance, if you have your value in a Variable called dTemp, you can diplay it in a MsgBox like that:

    Code:
    dTemp = 0.003521
    MsgBox "You precentage is: " & Format(dTemp, "#0.000%")
    Yopu can experiment with the format itself to find what suits your needs.

  13. #13
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Calculating percent

    Hi,
    I think your formula is wrong. It should be:

    set units_cancel_percent = (units_cancelled/units_sent)*100

    This will give you the percent of cancelled orders.
    e.g.
    250 units sent
    25 units cancelled
    25/250 = 0.1
    0.1*100 = 10
    10% of the units were cancelled

    The only time you'd get a divide-by-zero error is if units_sent was zero.

    Al.

    A computer is a tool, not a toy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width