Tuesday, April 22, 2014

TSQL Tutorial: LIKE Condition

The TSQL LIKE condition allows you to use wildcards to perform pattern matching. The LIKE condition is used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.

The syntax for the TSQL LIKE Condition is:
 
expression LIKE pattern [ ESCAPE 'escape_character' ]

% wildcard (percent sign wildcard)

The first TSQL LIKE example that we will look at involves using the % wildcard (percent sign wildcard).

Let's explain how the % wildcard works in the TSQL LIKE condition. We want to find all of the customers whose name begins with 'Hew'.

SELECT customer_name
FROM customers
WHERE customer_name LIKE 'Hew%';

You can also using the % wildcard multiple times within the same string. For example,

SELECT customer_name
FROM customers
WHERE customer_name LIKE '%bob%';

In this TSQL LIKE condition example, we are looking for all customers whose name contains the characters 'bob'.

 _ wildcard (underscore wildcard)

Next, let's explain how the _ wildcard (underscore wildcard) works in the TSQL LIKE condition. Remember that _ wildcard is looking for only one character.

For example:

SELECT last_name
FROM customers
WHERE last_name LIKE 'Sm_th';

This TSQL LIKE condition example would return all customers whose last_name is 5 characters long, where the first two characters is 'Sm' and the last two characters is 'th'. For example, it could return customers whose last_name is 'Smith', 'Smyth', 'Smath', 'Smeth', etc.

Here is another example:

SELECT *
FROM customers
WHERE account_number LIKE '12317_';

You might find that you are looking for an account number, but you only have 5 of the 6 digits. The example above, would retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return customers whose account numbers are:

123170, 123171, 123172, 123173, 123174, 123175, 123176, 123177, 123178, 123179

the NOT Operator

Next, let's look at how you would use the TSQL NOT Operator with wildcards.

Let's use the % wilcard with the NOT Operator. You could also use the TSQL LIKE condition to find customers whose name does not start with 'T'.

For example:

 

SELECT supplier_name
FROM customers
WHERE supplier_name NOT LIKE 'T%';

By placing the NOT Operator in front of the TSQL LIKE condition, you are able to retrieve all customers whose supplier_name does not start with 'T'.

Escape Characters

It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in Oracle.

Let's say you wanted to search for a % or a _ character in the TSQL LIKE condition. You can do this using an Escape character.

Please note that you can only define an escape character as a single character (length of 1).

For example:

SELECT *
FROM customers
WHERE supplier_name LIKE '!%' escape '!';

This TSQL LIKE condition example identifies the ! character as an escape character. This statement will return all customers whose name is %.

Here is another more complicated example using escape characters in the TSQL LIKE condition.

SELECT *
FROM customers
WHERE supplier_name LIKE 'H%!%' escape '!';

This TSQL LIKE condition example returns all customers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.

You can also use the escape character with the _ character in the TSQL LIKE condition.

For example:

SELECT *
FROM customers
WHERE supplier_name LIKE 'H%!_' escape '!';

This TSQL LIKE condition example returns all customers whose name starts with H and ends in _. For example, it would return a value such as 'Hello_'

No comments:

Post a Comment