Fundamentals of Patterns Most or all of the criteria we have specified so far with the WHERE keyword had to exactly match the specified criterion. In some cases, you may not remember the exact value in records but you want to specify some type of approximation. To let you do this, the SQL provides the LIKE operator.
Practical Learning: Introducing Patterns
A Pattern that Exactly Matches a Value The most basic comparison performed on a string-based field is to find out whether it is equal to a certain string. We already know that this operation can be performed using the = operator. Here is an example: SELECT PropertyNumber,
City,
PropertyType,
PropertyNumber,
City,
PropertyType,
Condition,
Bedrooms,
Bathrooms,
FinishedBasement,
Stories,
MarketValue
FROM Properties
WHERE PropertyType = "single family";
The LIKE operator can be used to perform the same operation. To do this, use the following formula: WHERE column-name LIKE value Here is an example: SELECT PropertyNumber,
City,
PropertyType,
PropertyNumber,
City,
PropertyType,
Condition,
Bedrooms,
Bathrooms,
FinishedBasement,
Stories,
MarketValue
FROM Properties
WHERE PropertyType LIKE "single family";
This and the previous code produce the same result:
To make the operation easier to read, you can put it in parentheses. The above code can be written as follows: SELECT PropertyNumber, City, PropertyType, PropertyNumber, City, PropertyType, Condition, Bedrooms, Bathrooms, FinishedBasement, Stories, MarketValue FROM Properties WHERE (PropertyType LIKE "single family"); Practical Learning: Creating a Pattern that Matches a Value
Negating a Pattern As done with all Boolean operations, there are ways you can negate a LIKE operation. Probably the easiest way is to precede the expression with the NOT operator. Here is an example: SELECT StateName,
AreaSqrKms,
AdmissionUnionOrder,
Region
FROM States
WHERE Region NOT LIKE 'New England';
If you precede the expression with NOT, it is recommended that you put the section after NOT between parentheses. As an alternative, in most cases, you can also precede the LIKE keyword with NOT. Here is an example: SELECT StateName,
AreaSqrKms,
AdmissionUnionOrder,
Region
FROM States
WHERE NOT Region Like 'New England';
You can also negate a negation. Here is an example: SELECT StateName,
AreaSqrKms,
AdmissionUnionOrder,
Region
FROM States
WHERE NOT States.Region NOT Like 'New England';
If you negate a negation, you can get the result as though none of both negations was used. Practical Learning: Negating a Pattern
Sorting Records If you are visually creating a query, you can sort the records using the Sort combo box in the bottom side of the window and for the field of your choice. Patterning by a Wildcard Matching a Character The idea of using a LIKE operator is to give an approximation of the value you want to compare to a field. To formulate this approximation, you use some specific symbols referred to as wildcards. They are combined with the LIKE operator. If you want to match any character, in any combination, for any length, use the * wildcard. If you precede it with a letter, as in n*, the condition would consist of finding any string that starts with that letter, in this case n. If you want to apply the search to multiple occurrences of a character, use as many combinations of the letter delimited by *. Remember that you can negate a LIKE condition by preceding it with NOT. Here is an example: Practical Learning: Matching Any Character
|
Matching a Single Character The * symbol on a LIKE operation is used to find many characters on the left or the right side of its accompanying character. If you want to find only one character, use the ? wildcard. For example, if you don't know by what character a string starts but know one or more characters after it, you can use ? followed by a sub-string. An example would be LIKE '?ane'. This would produce such strings as Cane, Lane, or Sane. If you don't know by what character a string starts, know the second character, but don't remember the rest of the characters, you can combine the ? and the * symbols. Remember that you can negate a LIKE condition by preceding it with NOT. Practical Learning: Matching a Single Character
Matching Specific Characters Instead of one specific character, you may want to match many. To do this, use the square brackets [] as the placeholder. Inside the brackets, enter the characters separated by commas. Outside the brackets, apply the * wildcard on either or both sides. Practical Learning: Matching Specific Characters
Matching a Range of Alphabetic Characters A range of alphabetic characters is a list of letters that consecutively follow each other in the alphabet. To let you match the letters that belong to the same range in the alphabet, include the square-brackets [] in the the LIKE string. Inside the brackets, the first letter, followed by -, and followed by the last letter of the range. On any or both parts of the square-brakets, apply the * wildcard using its rules. Consider the following example: SELECT AtomicNumber,
Symbol,
ElementName,
AtomicWeight
FROM Elements
WHERE ElementName Like '[c-g]*';
In this case, the result will include all chemical elements whose names start with C, D, E, F, or G. This would produce:
Practical Learning: Matching a Range of Characters
Matching a Sub-String Besides a single character, the * wildcard can be applied to a group of letters, called a sub-string. The rules are the same for a single character SELECT StateName, AreaSqrKms, AdmissionUnionOrder, Region FROM States WHERE Region Not Like '*north*'; Practical Learning: Matching a Sub-String
Negating a Wildcard Expression As opposed to considering the characters that are in a specific range, to specify a character, some characters, a range of characters, or a sub-string that must not be considered, you have two options. As seen already, you can precede the LIKE expression with the NOT operator. As an alternative, use the ! character inside the square brackets but before the character(s). Practical Learning: Negating a Pattern
Matching a Date/Time If the value you want to find is a date, include it between two # signs. Practical Learning: Matching a Date Value
|