<?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="Define complex search conditions" /> <meta name="abstract" content="In addition to the basic comparison predicates (=, >, <, and so on), a search condition can contain any of the predicates BETWEEN, IN, EXISTS, IS NULL, and LIKE." /> <meta name="description" content="In addition to the basic comparison predicates (=, >, <, and so on), a search condition can contain any of the predicates BETWEEN, IN, EXISTS, IS NULL, and LIKE." /> <meta name="DC.subject" content="SELECT statement, performing complex search condition, BETWEEN keyword, EXISTS keyword, IN keyword, IS NULL keyword, LIKE keyword" /> <meta name="keywords" content="SELECT statement, performing complex search condition, BETWEEN keyword, EXISTS keyword, IN keyword, IS NULL keyword, LIKE keyword" /> <meta name="DC.Relation" scheme="URI" content="rbafytexas.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafyspeciallike.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafymultiplewhere.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafywhere.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafywhereexp.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafysubquery.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafyussisql.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafymultiplewhere.htm" /> <meta name="DC.Relation" scheme="URI" content="rbafyspeciallike.htm" /> <meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstch2pred.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="rbafycomsrch" /> <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>Define complex search conditions</title> </head> <body id="rbafycomsrch"><a name="rbafycomsrch"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Define complex search conditions</h1> <div><p>In addition to the basic comparison predicates (=, >, <, and so on), a search condition can contain any of the predicates BETWEEN, IN, EXISTS, IS NULL, and LIKE. </p> <div class="section"><p>A search condition can include a scalar fullselect. </p> </div> <div class="section"><p>For character, or UCS-2 or UTF-16 graphic column predicates, the sort sequence is applied to the operands before evaluation of the predicates for BETWEEN, IN, EXISTS, and LIKE clauses. </p> </div> <div class="section"><p>You can also perform multiple search conditions.</p> <ul><li><strong>BETWEEN ... AND ...</strong> is used to specify a search condition that is satisfied by any value that falls on or between two other values. For example, to find all employees who were hired in 1987, you can use this: <pre>… <strong> WHERE</strong> HIREDATE <strong>BETWEEN</strong> '1987-01-01' <strong>AND</strong> '1987-12-31'</pre> <p>The BETWEEN keyword is inclusive. A more complex, but explicit, search condition that produces the same result is: </p> <pre>… <strong>WHERE</strong> HIREDATE >= '1987-01-01' <strong>AND</strong> HIREDATE <= '1987-12-31'</pre> </li> <li><strong>IN</strong> says you are interested in rows in which the value of the specified expression is among the values you listed. For example, to find the names of all employees in departments A00, C01, and E21, you can specify: <pre>… <strong>WHERE</strong> WORKDEPT <strong>IN</strong> ('A00', 'C01', 'E21')</pre> </li> <li><strong>EXISTS</strong> says you are interested in testing for the existence of certain rows. For example, to find out if there are any employees that have a salary greater than 60000, you can specify: <pre><strong>EXISTS (SELECT * FROM</strong> EMPLOYEE <strong>WHERE</strong> SALARY > 60000)</pre> </li> <li><strong>IS NULL</strong> says that you are interested in testing for null values. For example, to find out if there are any employees without a phone listing, you can specify: <pre>… <strong>WHERE</strong> EMPLOYEE.PHONE <strong>IS NULL</strong></pre> </li> <li><strong>LIKE</strong> says you are interested in rows in which an expression is similar to the value you supply. When you use LIKE, SQL searches for a character string similar to the one you specify. The degree of similarity is determined by two special characters used in the string that you include in the search condition: <dl><dt class="dlterm">_</dt> <dd>An underline character stands for any single character.</dd> <dt class="dlterm">%</dt> <dd>A percent sign stands for an unknown string of 0 or more characters. If the percent sign starts the search string, then SQL allows 0 or more character(s) to precede the matching value in the column. Otherwise, the search string must begin in the first position of the column.</dd> </dl> <div class="note"><span class="notetitle">Note:</span> If you are operating on MIXED data, the following distinction applies: an SBCS underline character refers to one SBCS character. No such restriction applies to the percent sign; that is, a percent sign refers to any number of SBCS or DBCS characters. See <a href="../db2/rbafzmst02.htm">SQL Reference</a> in the <span class="keyword">iSeries™ Information Center</span> for more information about the LIKE predicate and MIXED data.</div> <p>Use the underline character or percent sign either when you do not know or do not care about all the characters of the column's value. For example, to find out which employees live in Minneapolis, you can specify: </p> <pre>… <strong>WHERE</strong> ADDRESS <strong>LIKE</strong> '%MINNEAPOLIS%'</pre> <p>SQL returns any row with the string MINNEAPOLIS in the ADDRESS column, no matter where the string occurs.</p> <p>In another example, to list the towns whose names begin with 'SAN', you can specify: </p> <pre>… <strong>WHERE</strong> TOWN <strong>LIKE</strong> 'SAN%'</pre> <p>If you want to find any addresses where the street name isn't in your master street name list, you can use an expression in the LIKE expression. In this example, the STREET column in the table is assumed to be upper case. </p> <pre>… <strong>WHERE UCASE</strong> (:address_variable) <strong>NOT LIKE</strong> '%'||STREET||'%'</pre> <p>If you want to search for a character string that contains either the underscore or percent character, use the ESCAPE clause to specify an escape character. For example, to see all businesses that have a percent in their name, you can specify:</p> <pre>… <strong>WHERE</strong> BUSINESS_NAME <strong>LIKE</strong> '%@%%' ESCAPE '@'</pre> The first and last percent characters in the LIKE string are interpreted as the normal LIKE percent characters. The combination '@%' is taken as the actual percent character.</li> </ul> </div> </div> <div> <ul class="ullinks"> <li class="ulchildlink"><strong><a href="rbafyspeciallike.htm">Special considerations for LIKE</a></strong><br /> Here are some special considerations for using LIKE.</li> <li class="ulchildlink"><strong><a href="rbafymultiplewhere.htm">Multiple search conditions within a WHERE clause</a></strong><br /> You can qualify your request further by coding a search condition that includes several predicates.</li> </ul> <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 class="relconcepts"><strong>Related concepts</strong><br /> <div><a href="rbafysubquery.htm" title="You can use subqueries in a search condition as another way to select your data. Subqueries can be used anywhere an expression can be used.">Use subqueries</a></div> <div><a href="rbafyussisql.htm" title="A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters.">Sort sequences and normalization in SQL</a></div> </div> <div class="relref"><strong>Related reference</strong><br /> <div><a href="rbafywhere.htm" title="The WHERE clause specifies a search condition that identifies the row or rows you want to retrieve, update, or delete.">Specify a search condition using the WHERE clause</a></div> <div><a href="rbafywhereexp.htm" title="An expression in a WHERE clause names or specifies something you want to compare to something else.">Expressions in the WHERE clause</a></div> <div><a href="rbafymultiplewhere.htm" title="You can qualify your request further by coding a search condition that includes several predicates.">Multiple search conditions within a WHERE clause</a></div> <div><a href="rbafyspeciallike.htm" title="Here are some special considerations for using LIKE.">Special considerations for LIKE</a></div> </div> <div class="relinfo"><strong>Related information</strong><br /> <div><a href="../db2/rbafzmstch2pred.htm">Predicates</a></div> </div> </div> </body> </html>