ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafynulls.htm

163 lines
7.7 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Handle null values" />
<meta name="abstract" content="A NULL value indicates the absence of a column value in a row." />
<meta name="description" content="A NULL value indicates the absence of a column value in a row." />
<meta name="DC.subject" content="SELECT statement, NULL value, example, clause, NULL value" />
<meta name="keywords" content="SELECT statement, NULL value, example, clause, NULL value" />
<meta name="DC.Relation" scheme="URI" content="rbafytexas.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbafynulls" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Handle null values</title>
</head>
<body id="rbafynulls"><a name="rbafynulls"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Handle null values</h1>
<div><p>A <em>NULL</em> value indicates the absence of a column value in
a row.</p>
<div class="section"><p>A null value is not the same as zero or all blanks. A null value
means unknown. Null values can be used as a condition in the WHERE and HAVING
clauses. For example, a WHERE clause can specify a column that, for some rows,
contains a null value. A basic comparison predicate using a column that contains
null values does not select a row that has a null value for the column. This
is because a null value is neither less than, equal to, nor greater than the
value specified in the condition. The IS NULL predicate is used to check for
null values. To select the values for all rows that contain a
null value for the manager number, you can specify:</p>
</div>
<div class="section"><pre><strong>SELECT</strong> DEPTNO, DEPTNAME, ADMRDEPT
<strong>FROM</strong> CORPDATA.DEPARTMENT
<strong>WHERE</strong> MGRNO <strong>IS NULL</strong></pre>
<p>The result are:</p>
</div>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="all"><thead align="left"><tr><th align="left" valign="bottom" id="d0e56">DEPTNO</th>
<th align="left" valign="bottom" id="d0e58">DEPTNAME</th>
<th align="left" valign="bottom" id="d0e60">ADMRDEPT</th>
</tr>
</thead>
<tbody><tr><td align="left" valign="top" headers="d0e56 ">D01</td>
<td align="left" valign="top" headers="d0e58 ">DEVELOPMENT CENTER</td>
<td align="left" valign="top" headers="d0e60 ">A00</td>
</tr>
<tr><td valign="top" headers="d0e56 ">F22</td>
<td valign="top" headers="d0e58 ">BRANCH OFFICE F2</td>
<td valign="top" headers="d0e60 ">E01</td>
</tr>
<tr><td valign="top" headers="d0e56 ">G22</td>
<td valign="top" headers="d0e58 ">BRANCH OFFICE G2</td>
<td valign="top" headers="d0e60 ">E01</td>
</tr>
<tr><td valign="top" headers="d0e56 ">H22</td>
<td valign="top" headers="d0e58 ">BRANCH OFFICE H2</td>
<td valign="top" headers="d0e60 ">E01</td>
</tr>
<tr><td valign="top" headers="d0e56 ">I22</td>
<td valign="top" headers="d0e58 ">BRANCH OFFICE I2</td>
<td valign="top" headers="d0e60 ">E01</td>
</tr>
<tr><td valign="top" headers="d0e56 ">J22</td>
<td valign="top" headers="d0e58 ">BRANCH OFFICE J2</td>
<td valign="top" headers="d0e60 ">E01</td>
</tr>
</tbody>
</table>
</div>
<div class="section"><p>To get the rows that do not have a null value for the manager
number, you can change the WHERE clause like this:</p>
</div>
<div class="section"><pre><strong>WHERE</strong> MGRNO <strong>IS NOT NULL</strong></pre>
</div>
<div class="section"><p>Another predicate that is useful for comparing values that can
contain the NULL value is the DISTINCT predicate. Comparing two columns using
a normal equal comparison (COL1 = COL2) will be true if both columns contain
an equal non-null value. If both columns are null, the result will be false
since null is never equal to any other value, not even another null value.
Using the DISTINCT predicate, null values are considered equal. So (COL1 is
NOT DISTINCT from COL2) will be true if both columns contain an equal non-null
value and also when both columns are the null value.</p>
</div>
<div class="section"><p>For example, suppose you want to select information from 2 tables
that contain null values. The first table (T1) has a column (C1) with the
following values: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr valign="top"><th valign="top" id="d0e126">C1</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e126 ">2</td>
</tr>
<tr><td valign="top" headers="d0e126 ">1</td>
</tr>
<tr><td valign="top" headers="d0e126 ">null</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>The second table (T2) has a column (C2) with the following values: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr valign="top"><th valign="top" id="d0e146">C2</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e146 ">2</td>
</tr>
<tr><td valign="top" headers="d0e146 ">null</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>Run the following SELECT statement: </p>
<pre><strong>SELECT *
FROM </strong>T1, T2
<strong>WHERE </strong>C1 <strong>IS DISTINCT FROM</strong> C2</pre>
<p>The results are:</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr valign="top"><th valign="top" width="50%" id="d0e176">C1</th>
<th valign="top" width="50%" id="d0e178">C2</th>
</tr>
</thead>
<tbody><tr><td valign="top" width="50%" headers="d0e176 ">1</td>
<td valign="top" width="50%" headers="d0e178 ">2</td>
</tr>
<tr><td valign="top" width="50%" headers="d0e176 ">1</td>
<td valign="top" width="50%" headers="d0e178 ">-</td>
</tr>
<tr><td valign="top" width="50%" headers="d0e176 ">2</td>
<td valign="top" width="50%" headers="d0e178 ">-</td>
</tr>
<tr><td valign="top" width="50%" headers="d0e176 ">-</td>
<td valign="top" width="50%" headers="d0e178 ">2</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>For more information about the use of null values, see the <a href="../db2/rbafzmst02.htm">SQL Reference</a> topic
collection.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafytexas.htm" title="Learn a variety of ways of tailoring your query to gather data using the SELECT statement. One way to do this is to use the SELECT statement in a program to retrieve a specific row (for example, the row for an employee). Furthermore, you can use clauses to gather data in a specific way.">Retrieve data using the SELECT statement</a></div>
</div>
</div>
</body>
</html>