fhg
Volume 10, Number 15 -- May 12, 2010

Two Ways to Prevent Division by Zero in SQL

Published: May 12, 2010

by Ted Holt

Everybody above the age of 8 knows that division by zero is a no-no, or at least they should. Everybody who's used SQL for any amount of time knows that we can use a CASE expression to prevent division by zero. But do you know the other way to prevent division by zero in SQL?

Assume a database file called SomeTable, with fields called Quantity and Amount. Let's divide Amount by Quantity. First, here's the usual case statement.

select itnbr, Amount, Quantity,
       case when Quantity <> 0
               then Amount / Quantity
            else 0 end
  from SomeTable

If Quantity is not zero, the division takes place. If Quantity is zero, the calculation yields a zero. We could just as easily return any other numeric value, the value one, for example.

select itnbr, Amount, Quantity,
       case when Quantity <> 0
               then Amount / Quantity
            else 1 end
  from SomeTable

The other way to prevent division by zero is to use the NULLIF function. NULLIF requires two arguments. If the arguments are equal, NULLIF returns a null value. If they are not equal, NULLIF returns the first value.

Here's the SQL.

select itnbr, Amount, Quantity,
       Amount / nullif(Quantity,0)
  from SomeTable

If Quantity is not zero, the two arguments do not match, and the resulting calculation is Amount divided by Quantity. But if Quantity is zero, the arguments do match, and the calculation is Amount divided by null, which yields a null value.

If you use this technique, be sure to put the zero in the second argument of NULLIF.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SEQUEL SOFTWARE

SEQUEL ViewPoint®--Data Access & Analysis
for Power Systems™ Servers

                                               · Easy to use by IT and end users
                                               · Real-time data access and analysis
                                               · Queries, reports, pivot tables, drill-down analysis, kpi dashboards
                                               · IBM i-centric with multiplatform data access
                                               · Expert support and training
                                               · Secure data access
                                               · GUI, Web & green screen interface

SEQUEL--Fast, efficient & cost-effective data analysis

Click here for a FREE Information Kit!


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Help/Systems:  Robot/SCHEDULE Enterprise for UNIX, Linux, Windows & i
looksoftware:  RPG Open Access Webinar - May 18 at 10am (GMT) & May 19 at 2pm (EDT)
Essex Technology Group:  May 18-20: IBM POWER7 + COGNOS + VISION, NYC + PA + NJ


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
In Orlando, Optimism Returns

Open Access for RPG Grabs Attention at COMMON

Let's Take Another Stab at Power7 Blade Bang for the Buck

Mad Dog 21/21: Hot Deals and the Cool Server Nurseries

IBM Buys Integration Appliance Maker Cast Iron

Four Hundred Stuff
Maximum Availability Unveils New HA Monitor

Profound Brings RPG:OA-Like Features to V5R3 and V5R4

.NET App Modernization Tool Unveiled by looksoftware

NGS Makes OLAP Module Easier to Use

Raz-Lee Gets the Twitter Bug

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
May 1, 2010: Volume 12, Number 18

April 24, 2010: Volume 12, Number 17

April 17, 2010: Volume 12, Number 16

April 10, 2010: Volume 12, Number 15

April 3, 2010: Volume 12, Number 14

March 27, 2010: Volume 12, Number 13

TPM at The Register
BI benchmark outs HP Superdome 2 details

Xsigo scales down server I/O virtualizer

US economy adds 290,000 jobs in April

Open source R in commercial Revolution

Teradata's 2010 starts with a bang

HP software lands ex-Microsoft Windows and Office chief

SGI chills new Altix ICE supers

SGI books big(ger) loss in Q3

Cray revenues slammed in Q1

Nvidia's Fermi hits flop-hungry challengers

Cloud.com takes on virty infrastructure

TPC starts designing server virt test

THIS ISSUE SPONSORED BY:

ProData Computer Services
SEQUEL Software
WorksRight Software


Printer Friendly Version


TABLE OF CONTENTS
Development Environments

Two Ways to Prevent Division by Zero in SQL

Admin Alert: Diary of a Production System Upgrade, Part 2

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2010 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement