In our introduction to filtering in the previous lesson, we saw how to select a particular value from a column and get a list of only the records that include that value for that column. We also saw that, using Filter By Form, we could select a value from a combo box under a column header, apply that filter, and get the records that include that value. All these techniques actually allowed us to apply SQL visually. The SQL supports this filtering through an operator named WHERE.
In SQL, the WHERE operator is used to specify what value, from a column, to apply to a filter. The formula to follow is: SELECT What FROM WhatObject WHERE Condition; From what we learned already, the new operator in this formula is WHERE and it is required. The Condition is formulated as a logical comparison in the form: FieldName Operator Value The FieldName is the name of the field on which to apply the filtering. The Operator factor is a Boolean operator. Depending on the operator and your intentions, the Value can be one of the values in the FieldName or it can be a value not necessarily in the field. There are various ways you can visually use the WHERE operator:
Besides using WHERE in data analysis, we have already seen that if you want to save the results of data analysis, you should store them in a query. We also saw that you could create a SQL expression and use it as the Record Source of a form or a report. Therefore, after opening a query in Design View or the Query Builder for a form or report, select the desired column(s). In the bottom section, click the Criteria box of the column that holds the values and type the desired value.
When creating a query that creates a list of records based on a particular value, the presence of the column that holds that value may become redundant. For example, if you create a list of real estate properties that includes only single family homes, and if it is evident to the user that the list includes only those properties, there is no point to still include that column in the view. This means that the column that holds the WHERE condition can be left out of the expression displaying it. This technique also applies to the other SQL operators we will study in this and the next lessons. To visually remove the column that holds a condition, in the Design View of the query, you can clear its Show check box.
In the previous two lessons, we saw that you could filter by value between two other values, in a range. The SQL supports this technique through an operator named BETWEEN. The BETWEEN operator is combined with the AND operator to get a list of records between two values. Its formula is: WHERE FieldName BETWEEN Value1 AND Value2 The WHERE, BETWEEN, and AND operators are required. The FieldName must be one of the fields on the table. The first value represents the lowest (or highest) value in the range while the second value represents the highest (or lowest) value of the range. To visually filter BETWEEN:
If you want to save the list, start a query and display it in Design View. Select the column(s) you want to use. In the Criteria box that corresponds to that column, enter the the BETWEEN Value1 AND Value2 expression. Then save the query.
When studying filtering by value, we saw that you could display a list of the values of a certain column, clear the check boxes of the values you don't want, keep the check boxes on the values you want, and apply the criteria. That was a technique of working on a range of values. To support this, the SQL allows you to create a list of values from a column and get only the records that include one of those values. This is done using the IN operator. After the SELECT expression of the statement, the formula to use IN is: IN(Value1, Value2, Value_n) The IN operator is followed by parentheses. Inside the parentheses, type each of the desired values from the column on which to apply the filter, separated by commas.
To create an IN operation:
If you want to save the list, start a query and display it in Design View. Select the column(s) you want to use. In the Criteria box that corresponds to that column, enter the IN expression.
In Lesson 22, we saw that when a column uses strings, sometimes you may know an approximation of a string you are looking for, or you may want to filter based on a range of characters. For example, you may not remember the exact first name of a student you are looking for but you think the name ends with "ette", like Jeannette, Bernadette, Pierrette, or Paulette. In another case, you may know only the character by which a name starts, or you may know a substring in the name you are looking for. The SQL supports these types of filtering using an operator named LIKE.
The LIKE operator uses some special characters or symbols called wildcards. To start:
If you want to save the result, start a query in Design View, select the column(s) you want to use. Then, in the Criteria box that corresponds to that column, type the LIKE expression. To use a LIKE operation in SQL, after the SELECT expression, type LIKE followed by the intended expression. When you apply the filter, all records that abide by the expression would be in the result.
The simplest way to use LIKE is to apply a string to it, the same way you would use a filter by selection. For example, if you have a list of people and one of them is named "Moore" or some of them are named "Moore" and you want to find the record(s) with that name, you would use LIKE "Moore".
If the value you want to find is a date, include it between two # signs as in LIKE #8/8/1993#.
The most regular use of the LIKE is with strings. To get a list of strings that include any characters as a substring in a string, use the * symbol. For example, if you do not know how the string starts but you know how it ends, type LIKE "*". On the right side of *, type the substring that the string should end with. Here is an example:
If you know how the string starts but do not know how it ends, precede the substring with *. An example would be LIKE "jean*". This would produce records that have a string that starts with jean regardless of how it ends. Examples would be Jean, Jeannette, Jeanot. If you do not know how the string starts or how it ends but know one or more characters that it includes, surround the substring with * symbols. Here is an example:
You may want to use a range of characters to filter by. For example, imagine you want to find the strings that start with characters in the range from d to g. To specify the range, use [] as a placeholder for the range. Inside the square brackets, type the first character of the desired range, followed by -, and followed by the last character of the desired range. Outside the [] placeholder, use the * character to specify that the rest would precede, would follow, or both. For example, to specify that you want the strings that start with any character from o to r followed by anything, you would use LIKE "[o-r]*":
In the same way, as stated already, you can use the * to the left, to the right or both. Also, you can combine the [] placeholder with the other wildcards.
Instead of using a range of characters, you may want to exclude the characters of a specific range. To negate the filter of a range of characters, you can precede the LIKE operation with the NOT operator. An example would be NOT LIKE "[c-w]*":
As an alternative, you can start the inside of the [] placeholder with !. An example would be LIKE "[!o-r]*". This would result in all records that do not start with o, p, or r.
Instead of finding a range of characters, you may want to use specific characters. To do this, use the [] as the placeholder. Inside the square brackets, enter the characters separated by commas. An example would be LIKE "[a, h, o, y]*":
To negate the filter, you can precede the LIKE operation with the NOT operator. An example would be NOT LIKE "[a, h, o, y]*". As an alternative, inside the [] placeholder, start with the ! symbol. An example would be LIKE "[!a, h, o, y]*". In both cases, the list would include records that do not start with a, h, o, or y.
The * character on a LIKE operation is used to find many characters. If you want to find only one character, use the ? wildcard. For example, if you do not know by what character a string starts but know one or more characters after it, you can use ? followed by a substring. An example would be LIKE "?ane". This would produce such strings as Pane, Bane, or Sane. If you do not know by what character a string starts, know the second character, but do not remember the rest of the characters, you can combine the ? and the * symbols. An example would be LIKE "?i*":
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|