Database Articles

  Home arrow Database Articles arrow Page 5 - Power Selecting
DATABASE ARTICLES

Power Selecting
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2004-12-20

    Table of Contents:
  • Power Selecting
  • Order By
  • Distinct
  • As
  • Like
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Power Selecting - Like


    (Page 5 of 6 )

    In a SELECT statement, the keyword WHERE may be used to set forth conditions which restrict what records are returned. Searching for and retrieving records where a field's value may only partially be known can be done with the LIKE operator.

    Pattern matching with LIKE makes use of two special characters: the underscore (_) and the percent (%).

    An underscore is used when only one character is needed to match. Consider the following example which selects Jose and Josh from the table but not Joseph.

    SELECT * FROM Clients WHERE FirstName LIKE 'Jos_';

    LastName FirstName Address               City            State ZipCode
    ----------------------------------------------------------------------
    Chavez   Jose      14 Quailbush Dr       Jasper          TN    37347
    Smith    Josh      52 Harding Ln         Franklin        MA    02038

    The WHERE/LIKE condition matches records that have a FirstName column starting with Jos followed by one more character. In this case, Jose and Josh are retrieved, but not Joseph.

    The percent sign is used to match zero or more characters, as illustrated by the next example.

    SELECT * FROM Clients WHERE FirstName LIKE 'Jos%';

    LastName FirstName Address               City            State ZipCode
    ----------------------------------------------------------------------
    Chavez   Jose      14 Quailbush Dr       Jasper          TN    37347
    Adams    Joseph    214 Main St           Seattle         WA    98102
    Smith    Josh      52 Harding Ln         Franklin        MA    02038

    The WHERE/LIKE condition matches records that have a value in the FirstName column that starts with Jos but is either followed by zero or more characters. In this case Jose, Joseph and Josh are all returned.

    Remember, the percent character matches zero or more characters, so a search query for Jose% would return both Jose and Joseph.

    Matching characters are not restricted to the end of character strings. Consider the following examples:

    SELECT * FROM Clients where Address LIKE '% Main %';

    LastName FirstName Address               City            State ZipCode
    ----------------------------------------------------------------------
    Adams    Joseph    214 Main St           Seattle         WA    98102
    Smith    Aaron     9112 Main St          San Diego       CA    92119

    SELECT * FROM Clients where Address LIKE '% St';

    LastName FirstName Address               City            State ZipCode
    ----------------------------------------------------------------------
    Adams    Gwendolyn 205 W Third St        Brownville      AL    35020
    Smith    Harold    321 Elm St            Portsmouth      RI    02871
    Adams    Joseph    214 Main St           Seattle         WA    98102
    Copland  Dorothy   639 Spring St         West Sacramento CA    95691
    Smith    Aaron     9112 Main St          San Diego       CA    92119

    SELECT * FROM Clients where Address LIKE '_ _ %';

    LastName FirstName Address               City            State ZipCode
    ----------------------------------------------------------------------
    Chavez   Jose      14 Quailbush Dr       Jasper          TN    37347
    Johnson  Diane     82 Richardson Ave     Fresno          CA    93702
    Smith    Josh      52 Harding Ln         Franklin        MA    02038

    Since pattern matching characters can be used anyplace in the WHERE criteria, any substring found within the record's field can be selected.

    More Database Articles Articles
    More By bluephoenix

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap