Overview of expressions
You can use an expression anywhere in a query where you can use a column name. Expressions can calculate values to display, be part of search conditions, or combine the contents of data columns. An expression can consist of a mathematical calculation or a string, and can involve any combination of column names, literals, operators, or functions.
Examples of the use of expressions in a query include:
products
table, displaying a discounted price that is calculated by taking 10% off the retail price.orders
table and a products
table, then sorting the query on the total price (order quantity times product price).orders
table, copying and then deleting all orders that were shipped more than one year ago.Expression structure and expression examples
General guidelines for creating expressions
An expression consists of any combination of column names, literals, operators, or functions. Follow these guidelines in combining elements to form expressions:
job_id
qualified with the table name employee
: employee.job_id
Note The Query Designer can accept terms in double quotation marks, but might interpret them differently than you expect. In a Microsoft Access Project, the Query Designer always interprets double quotation marks as database object delimiters.
Expression examples
The following table illustrates the use of expressions in a query.
Expression | Result |
---|---|
SELECT (price * .9) |
Displays a discounted price (10% off the value in the price column). |
SELECT (lname + ', ' + fname) |
Displays the concatenated values of the last name and first name columns with a comma between them. |
SELECT sales.qty, titles.price
|
After joining two tables, sorts the result set by the total value of an order (quantity times price). |
SELECT au_lname, au_fname |
Displays authors whose area code is in the San Francisco area. |
SELECT ord_num, ord_date |
Finds all orders in the sales table that were made in the last 10 days. TodayÆs date is returned by the GETDATE( ) function. |
Operators for expressions
You can use a variety of operators in constructing expressions for your query, including mathematical and text operators.
Mathematical operators
The following table lists the mathematical operators you can use in constructing an expression.
Operator | Meaning |
---|---|
+, - | Unary positive, negative |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
If you use more than one mathematical operator in an expression, the Query Designer processes the expression according to the following operator precedence. To override the default precedence, use parentheses around the portion of the expression that is to be evaluated first. If more than one operator of the same level is included, the operators are evaluated left to right.
Text operator
You can perform one operation on text: concatenation or the linking together of strings. You can concatenate strings and perform other operations (such as removing extraneous spaces) using a single operator. To concatenate a string, you can use the "+" operator in the Grid pane.
Predefined variables for expressions
In addition to using column names, literals, operators, and functions in an expression, you can use predefined variables that have defined meanings or values. For example, you can use a predefined variable to display the user name for the current user or to search for data columns that contain no value (null).
The list below includes examples of predefined variables.
Predefined Variable | Description | Example |
---|---|---|
CURRENT_USER | The user name of the current user |
Changes the |
NULL | A null value, used in the search expressions IS NULL and IS NOT NULL |
Finds authors who have no middle initial. Note The keyword IS is a special operator for use with NULL. |