The
LIKE
predicate
T-SQL provides a predicate called LIKE that allows you to check whether a character string matches a
specified pattern. Similar patterns are used by the PATINDEX function described earlier. The following
section describes the wildcards supported in the patterns and demonstrates their use.
The
%
(percent) Wildcard
The percent sign represents a string of any size, including an empty string. For example, the following
query returns employees where the last name starts with D.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'D%';
This query returns the following output.
empid lastname
----------- --------------------
1 Davis
9 Dolgopyatova
Note that often you can use functions such as SUBSTRING and LEFT instead of the LIKE predicate
to represent the same meaning. But the LIKE predicate tends to get optimized better—especially
when the pattern starts with a known prefix.
www.it-ebooks.info
72
Microsoft SQL Server 2012 T-SQL Fundamentals
Do'stlaringiz bilan baham: |