Oracle
  Home arrow Oracle arrow Page 7 - Mastering the WHERE Clause
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Mastering the WHERE Clause
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2004-10-19

    Table of Contents:
  • Mastering the WHERE Clause
  • WHERE to the Rescue
  • WHERE Clause Evaluation
  • Conditions and Expressions
  • Membership Conditions and Range Conditions
  • Matching Conditions
  • Regular Expressions and Handling NULL
  • Placement of Join Conditions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Mastering the WHERE Clause - Regular Expressions and Handling NULL


    (Page 7 of 8 )

    Beginning with the Oracle Database 10g release, you can use regular expressions within your conditions. Regular expressions allow for much more complex pattern matching without the need for multiple conditions. For example, if you wanted to find all customers whose name begins with W, ends in “ies” and does not include L anywhere in the string, you could use multiple conditions with the LIKE and NOT LIKE operators:

    SELECT name
    FROM customer
    WHERE name LIKE 'W%ies'
      AND name NOT LIKE '%L%';

    NAME
    ----------------------------------
    Worcester Technologies
    Wallace Industries

    You can achieve the same result more succinctly, in a single expression, with the new REGEXP_LIKE function:

    SELECT name
    FROM customer
    WHERE REGEXP_LIKE(name, '^W([^L]*)ies$');

    NAME
    -------------------------
    Worcester Technologies
    Wallace Industries

    If that second argument to REGEXP_LIKE looks like gibberish, fear not: we cover regular expressions in detail in Chapter 17.

    Handling NULL

    The NULL expression represents the absence of a value. If, when entering an order into the database, you are uncertain when the order will be shipped, it is better to leave the ship date undefined than to fabricate a value. Until the ship date has been determined, therefore, it is best to leave the ship_dt column NULL. NULL is also useful for cases where data is not applicable. For example, a cancelled order’s shipping date is no longer applicable and should be set to NULL.

    When working with NULL, the concept of equality does not apply; a column may be NULL, but it will never equal NULL. Therefore, you will need to use the special operator IS NULL when looking for NULL data, as in:

    UPDATE cust_order
    SET expected_ship_dt = SYSDATE + 1
    WHERE ship_dt IS NULL;

    In this example, all orders whose shipping date hasn’t been specified will have their expected shipping date set to tomorrow.

    You may also use the IS NOT NULL operator to locate non-NULL data:

    UPDATE cust_order
    SET expected_ship_dt = NULL
    WHERE ship_dt IS NOT NULL;

    This example sets the expected shipping date to NULL for all orders that have already shipped. Notice that the SET clause uses the equality operator (=) with NULL, whereas the WHERE clause uses the ISNOT NULL operator. The equality operator is used to set a column to NULL, whereas the ISNOT NULL operator is used to evaluate whether a column is NULL. A great many mistakes might have been avoided had the designers of SQL chosen a special operator to be utilized when setting a column to NULL (i.e., SET expected_ship_dt TO NULL), but this is not the case. To make matters worse, Oracle doesn’t complain if you mistakenly use the equality operator when evaluating for NULL. The following query will parse and execute but will never return rows:

    SELECT order_nbr, cust_nbr, sale_price, order_dt
    FROM cust_order
    WHERE ship_dt
    = NULL;

    Hopefully, you would quickly recognize that the previous query never returns data and replace the equality operator with ISNULL. However, there is a more subtle mistake involving NULL that is harder to spot. Say you are looking for all employees who are not managed by Marion Blake, whose employee ID is 7698. Your first instinct may be to run the following query:

    SELECT fname, lname, manager_emp_i d
    FROM employee
    WHERE manager_emp_id != 7698;

    FNAME          LNAME       MANAGER_EMP_ID
    -------------- ----------- ---------------
    JOHN           SMITH       7902
    TERRY          JONES       7839
    MARION         BLAKE       7839
    CAROL          CLARK       7839
    DONALD         SCOTT       7566
    DIANE          ADAMS       7788
    JENNIFER       FORD        7566
    BARBARA        MILLER      7782

    While this query returns rows, it leaves out those employees who are top-level managers and, thus, are not managed by anyone. Since NULL is neither equal nor not equal to 7698, this set of employees is absent from the result set. To ensure that all employees are considered, you will need to explicitly handle NULL, as in:

    SELECT fname, lname, manager_emp_id
    FROM employee
    WHERE manager_emp_id IS NULL OR manager_emp_id != 7698;

    FNAME          LNAME       MANAGER_EMP_ID
    -------------- ----------- ---------------
    JOHN           SMITH       7902
    TERRY          JONES       7839
    MARION         BLAKE       7839
    CAROL          CLARK       7839
    DONALD         SCOTT       7566
    FRANCIS        KING
    DIANE          ADAMS       7788
    JENNIFER       FORD        7566
    BARBARA        MILLER      7782

    Including two conditions for every nullable column in your WHERE clause can get a bit tiresome. Instead, you can use Oracle’s built-in function NVL, which substitutes a specified value for columns that are NULL, as in:

    SELECT fname, lname, manager_emp_i d
    FROM employee
    WHERE NVL(manager_emp_id, -999) != 7698;

    FNAME          LNAME       MANAGER_EMP_ID
    -------------- ----------- ---------------
    JOHN           SMITH       7902
    TERRY          JONES       7839
    MARION         BLAKE       7839
    CAROL          CLARK       7839
    DONALD         SCOTT       7566
    FRANCIS        KING
    DIANE          ADAMS       7788
    JENNIFER       FORD        7566
    BARBARA        MILLER      7782

    In this example, the value -999 is substituted for all NULL values, which, since -999 is never equal to 7698, guarantees that all rows whose manager_emp_id column is NULL will be included in the result set. Thus, all employees whose manager_emp_id column is NULL or is not NULL and has a value other than 7698 will be retrieved by the query.  

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More Oracle Articles
    More By O'Reilly Media


       · SELECT LLT_CODE FROM L_LOW_LEVEL_TERMWHERE (LLT_NAME='Hyperkalaemia' OR...
     

       

    ORACLE ARTICLES

    - Implementing and Using Oracle`s Restore Poin...
    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway