When you precede the % character with a letter, only that letter would be considered. Alternatively, you can specify a group of characters that would precede the % symbol. For example, if you have some first names that start with Ch in a list but you don't remember the end of the name you are looking for, to create the list, you can specify that the first name would start with Ch and end with whatever. In this case, you would use Ch% as follows: SELECT FirstName, LastName, Gender, SPHome FROM Students WHERE LastName LIKE N'Ch%' This would produce:
Instead of ending a letter or a group of letters with %, you can begin the LIKE statement with %. An example would be LIKE "%son". In this case, all strings that end with son, such as Johnson or Colson, would be considered. If you remember neither the beginning nor the end of a string you want to search for, but you know a sub-string that is probably included in the type of string you are looking for, you can precede it with % and end it with %. An example would be LIKE "%an%". In this case, all strings that include "an" anywhere inside would be considered. Here is an example: SELECT FirstName, LastName, Gender, SPHome FROM Students WHERE LastName LIKE '%an%' This would produce:
Like the other SQL statements, you can also negate this one using the NOT operator.
The % wildcard is used to precede or succeed a specific character or a group of characters, that is, any character. If you want to consider only a range of characters from the alphabet, you can include the range in square brackets. To do this, type [, followed by the lowest character of the range, followed by -, followed by the highest character of the range, followed by ]. For example, to consider the range of letters between p and s, you would use '[p-s]'. Then, either to the left, to the right, or to both sides of this expression, type % to specify whether to include any character or combination of characters before or after the expression. Here is an example: SELECT FirstName, LastName, Gender, SPHome FROM Students WHERE LastName LIKE N'%[p-s]' In the case, the result would be a list of students whose last names end with p, q, r, or s. This would produce:
Notice that the list includes only the students whose last names end with a letter between p and s.
As opposed to considering the characters that are in a specific range, to specify a character or a range of characters that must NOT be considered, use the ^ character inside the square brackets but before the desired range. Here is an example: SELECT FirstName, LastName, Gender, SPHome FROM Students WHERE (LastName LIKE '%[^p-r]') The result would be a list of students whose last end with a letter other than p, q, r, or s. Once again, remember that you can negate this expression by preceding it with NOT. Note that if you negate an expression that include ^, you would get the same result as not using ^. |
|
|||||
|
|
||
Home | Copyright © 2007-2009 FunctionX, Inc. | |
|