Using the LIKE Search Condition
The LIKE
search condition uses wildcards to search for patterns within a string.
The wildcards, however, are different from the MS-DOS wildcards with
which you may be familiar. Table 2 shows both the SQL and MS-DOS wildcards.
Table 2 SQL Wildcards
The next query uses the LIKE search condition to locate all products that begin with ‘Chain' optionally followed by any number of characters:
USE AdventureWorks;
SELECT Name
FROM Production.Product
WHERE
Name LIKE ‘Chain%'
Result:
Name
-------------------
Chain
Chain Stays
Chainring
Chainring Bolts
Chainring Nut
The following query finds any StateProvince name beginning with a letter between d and f, inclusive:
USE AdventureWorks;
SELECT Name
FROM Person.StateProvince
WHERE Name LIKE ‘[d-f]%';
Result:
Name
--------------------------------------------------
Delaware
District of Columbia
Dordogne
Drome
England
Essonne
Eure
Eure et Loir
Finistere
Florida
France
The two possible methods for searching for a
pattern that contains a wildcard are to either enclose the wildcard in
square brackets or put an escape character before it. The trick to the
latter workaround is that the escape character is defined within the LIKE expression.
When using the LIKE
operator, be aware that the database collation's sort order determines
both case-sensitivity and the sort order for the range of characters.
You can optionally use the keyword COLLATE to specify the collation sort order used by the LIKE operator.
Best Practice
Although the LIKE
operator can be useful, it can also cause a performance hit. Indexes
are based on the beginning of a column, not on phrases in the middle of
the column. If you find that the application requires frequent use of
the LIKE operator, you should
enable full-text indexing — a powerful indexing method that can even
take into consideration weighted words and variations of inflections
and can return the result set in table form for joining.
Multiple WHERE Conditions
You can combine multiple WHERE conditions within the WHERE clause using the Boolean logical operators: AND, OR, and NOT.
As with the mathematical operators of multiplication and division, an
order of precedence exists with the Boolean logical operators: NOT comes first, then AND, and then OR:
USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
Name LIKE ‘Chain%'
OR
ProductID BETWEEN 320 AND 324
AND
Name Like ‘%s%';
Result:
ProductID Name
----------- ---------------------
952 Chain
324 Chain Stays
322 Chainring
320 Chainring Bolts
321 Chainring Nut
With parentheses, the result of the query is radically changed:
USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
(
Name LIKE ‘Chain%'
OR
ProductID BETWEEN 320 AND 324
)
AND
Name Like ‘%s%';
Result:
ProductID Name
--------------- ---------------------
324 Chain Stays
320 Chainring Bolts
Although the two preceding queries are similar,
in the first query the natural order of precedence for Boolean
operators caused the AND to be evaluated before the OR. The OR included the Chains in the results.
The second query used parentheses to explicitly dictate the order of the Boolean operators. The OR collected the Chains and products with a ProductID of 952, 324, 322, 320, 321, or 323. This list was then ANDed with products that included the letter g in their names. Only products 320 and 324 passed both of those tests.
Best Practice
When coding complex Boolean or
mathematical expressions, explicitly stating your intentions with
parentheses and detailed comments reduces misunderstandings and errors
based on false assumptions.
SELECT … WHERE
Surprisingly, using the WHERE clause in a SELECT statement does not require the use of a FROM clause or any data source reference. A SELECT statement without a FROM clause returns a single row that includes any expressions in the SELECT's column list.
A WHERE clause on a nontable SELECT statement serves as a restriction to the entire SELECT statement. If the WHERE condition is true, the SELECT statement functions as expected:
SELECT ‘abc' AS col
WHERE 1>0;
Result:
col
----
abc
(1 row(s) affected)
If the WHERE condition is false, the SELECT statement still executes but it returns zero rows:
SELECT ‘abc' AS col WHERE 1<0;
Result:
col
----