Wildcard in Sql Server Select
the LIKE operator allows us to perform basic pattern-matching using wildcard characters. For Microsoft SQL Server, the wildcard characters are defined as follows:
Wildcard | Description |
---|---|
_ (underscore) | matches any single character |
% | matches a string of one or more characters |
[ ] | matches any single character within the specified range (e.g. [a-f]) or set (e.g. [abcdef]). |
[^] | matches any single character not within the specified range (e.g. [^a-f]) or set (e.g. [^abcdef]). |
A few examples should help clarify these rules.
- WHERE FirstName LIKE '_im' finds all three-letter first names that end with 'im' (e.g. Jim, Tim).
- WHERE LastName LIKE '%stein' finds all employees whose last name ends with 'stein'
- WHERE LastName LIKE '%stein%' finds all employees whose last name includes 'stein' anywhere in the name.
- WHERE FirstName LIKE '[JT]im' finds three-letter first names that end with 'im' and begin with either 'J' or 'T' (that is, only Jim and Tim)
- WHERE LastName LIKE 'm[^c]%' finds all last names beginning with 'm' where the following (second) letter is not 'c'.
0 comments:
Post a Comment