About Expressions in Queries

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:

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:

Expression examples

The following table illustrates the use of expressions in a query.

Expression Result
SELECT (price * .9)
FROM products

Displays a discounted price (10% off the value in the price column).
SELECT (lname + ', ' + fname)
FROM employee

Displays the concatenated values of the last name and first name columns with a comma between them.
SELECT sales.qty, titles.price

FROM sales INNER JOIN titles ON
sales.title_id = titles.title_id
ORDER BY
(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
FROM authors
WHERE
(SUBSTRING(phone, 1, 3) = '415')

Displays authors whose area code is in the San Francisco area.
SELECT ord_num, ord_date
FROM sales
WHERE
(ord_date >=
DATEADD(day, -10, GETDATE()))

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

Note   You can use additional operators available, such as the "%" operator to determine modulo or remainders in arithemtic operations. For more information about the modulo arithmetic operator, see the Microsoft SQL Server documentation.

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.

  1. Unary + and -
  2. * and /
  3. + and -

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
UPDATE accounts 
SET salesperson = CURRENT_USER
WHERE region = 'NW'

Changes the salesperson column for all accounts in the northwest region to the name of the current user.

NULL A null value, used in the search expressions
IS NULL and
IS NOT NULL
SELECT emp_id, lname, fname, minit
FROM employee
WHERE minit IS NULL

Finds authors who have no middle initial.

Note   The keyword IS is a special operator for use with NULL.