Collection Contents Index Compound search conditions in the WHERE clause Matching rows by sound pdf/chap12.pdf

First Guide to SQL Anywhere Studio
   PART 3. Basic SQL
     CHAPTER 12. Selecting Data from Database Tables       

Pattern matching in search conditions


Another useful way to look for things is to search for a pattern. In SQL, the word LIKE is used to search for patterns. The use of LIKE can be explained by example.

  To list all employees whose last name begins with BR:
  1. Type the following:

    SELECT emp_lname, emp_fname
    FROM employee
    WHERE emp_lname LIKE 'br%'

emp_lname

emp_fname

Breault

Robert

Braun

Jane

The % in the search condition indicates that any number of other characters may follow the letters BR.

  To list all employees whose surname begins with BR, followed by zero or more letters and a T, followed by zero or more letters:
  1. Type the following:

    SELECT emp_lname, emp_fname
    FROM employee
    WHERE emp_lname LIKE 'BR%T%'

emp_lname

emp_fname

Breault

Robert

The first % sign matches the string eaul, while the second % sign matches the empty string (no characters).

Another special character that can be used with LIKE is the _ (underscore) character, which matches exactly one character.

The pattern BR_U% matches all names starting with BR and having U as the fourth letter. In Braun the _ matches the letter A and the % matches N.


Collection Contents Index Compound search conditions in the WHERE clause Matching rows by sound pdf/chap12.pdf