SELECT CASE WHEN to avoid Divide by Zero

  • Hello:

    I think I might be close here, but I need to return 0 if there is division by zero.

    SELECT        JobInfo.JobNo, JobInfo.Product, JobInfo.ItemType, JobInfo.Detail, JobInfo.Started, JobInfo.Completed, JobInfo.Engineer, JobInfo.Designer, JobInfo.EstHrs, JobInfo.ShipTo, JobHrs.ActualHrs,

    CASE WHEN JobInfo.EstHrs > 0 THEN ((JobInfo.EstHrs / JobHrs.ActualHrs) * 100) AS Efficiency ELSE 0 AS Efficiency

    FROM            JobInfo INNER JOIN

    JobHrs ON JobInfo.JobNo = JobHrs.JobNo

    WHERE        (JobInfo.Completed BETWEEN @SDate AND @EDate) AND (JobInfo.Completed <> '1979-01-01')

    ORDER BY JobInfo.Designer

     

    Current logic produces:

    Error in SELECT clause: expression near 'AS'.

    Error in SELECT clause: expression near 'FROM'.

    Missing FROM clause.

    Unable to parse query text.

    Thanks..

     

     

    Steve Anderson

  • your CASE statement needs an END on it before the AS.

    That should fix it for you!

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Try this

    SELECT JobInfo.JobNo
    ,JobInfo.Product
    ,JobInfo.ItemType
    ,JobInfo.Detail
    ,JobInfo.Started
    ,JobInfo.Completed
    ,JobInfo.Engineer
    ,JobInfo.Designer
    ,JobInfo.EstHrs
    ,JobInfo.ShipTo
    ,JobHrs.ActualHrs
    ,Efficiency = CASE
    WHEN JobInfo.EstHrs > 0 THEN
    (JobInfo.EstHrs * 100 / JobHrs.ActualHrs)
    ELSE
    0
    END
    FROM JobInfo
    JOIN JobHrs
    ON JobInfo.JobNo = JobHrs.JobNo
    WHERE (JobInfo.Completed
    BETWEEN @SDate AND @EDate
    )
    AND (JobInfo.Completed <> '1979-01-01')
    ORDER BY JobInfo.Designer;

    Not sure why your >0 test is on EstHrs, when you are dividing by ActualHrs, but I'm assuming this logic makes sense to you.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Typically people use a NULLIF like below, to avoid a CASE statement -- whether you prefer that or not is, of course, up to you:

    ISNULL(((JobInfo.EstHrs / NULLIF(JobHrs.ActualHrs, 0)) * 100), 0)

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Hello:

    Just one more thing,  how to I get a display of fewer decimals?  It seems to want to show 8 plus decimals, even if I apply round, just converting them to zero.

    I'm about tutorialed out on this.

    SELECT JobInfo.JobNo, JobInfo.Product, JobInfo.ItemType, JobInfo.Detail, JobInfo.Started, JobInfo.Completed, JobInfo.Engineer, JobInfo.Designer, JobInfo.EstHrs, JobInfo.ShipTo, JobHrs.ActualHrs, Efficiency = CASE

    WHEN JobInfo.EstHrs > 0 THEN

    (JobHrs.ActualHrs / JobInfo.EstHrs) * 100 AS NUMERIC(5, 0)

    ELSE

    0 AS NUMERIC(5, 0)

    END

    FROM JobInfo INNER JOIN

    JobHrs ON JobInfo.JobNo = JobHrs.JobNo

    WHERE (JobInfo.Completed BETWEEN @SDate AND @EDate) AND (JobInfo.Completed <> '1979-01-01')

    ORDER BY JobInfo.Designer

     

    Thanks

     

    Steve Anderson

  •       ,Efficiency = CASE
    WHEN JobInfo.EstHrs > 0 THEN
    CAST((JobInfo.EstHrs * 100 / JobHrs.ActualHrs) AS Decimal(19,2))
    ELSE
    0
    END

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thank you!

    Steve Anderson

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply