Expressions (T-SQL)

An expression is a combination of symbols and operators that Microsoft® SQL Server™ evaluates to obtain a single data value. Simple expressions can be a single constant, variable, column, or scalar function. Operators can be used to join two or more simple expressions into a complex expression.

Syntax

{    constant
    
| scalar_function
    
| [alias.]column
    
| local_variable
    
| (expression)
    | (scalar_subquery)
    
| {unary_operator}expression
    | expression {binary_operator} expression
}

Arguments
constant
Is a symbol that represents a single, specific data value. constant is one or more alphanumeric characters (letters a-z and A-Z) or symbols (!, @, #, and so on). Character and datetime values are enclosed in quotation marks, binary strings and numeric constants are not. For more information, see Constants.
scalar_function
Is a unit of Transact-SQL syntax that provides a specific service and returns a single value, such as the SUM, GETDATE, or CAST functions.
[alias.]
Is the alias, or correlation name, assigned to a table by the AS keyword in the FROM clause.
column
Is the name of a column. Only the name of the column is allowed in an expression; a four-part name cannot be specified.
local_variable
Is the name of a user-defined variable. For more information, see DECLARE @local_variable.
(expression)
Is any valid SQL Server expression as defined in this topic. The parentheses are grouping operators that ensure that all the operators in the expression within the parentheses are evaluated before the resulting expression is combined with another.
(scalar_subquery)
Is a subquery that returns one value, for example:

SELECT MAX(UnitPrice)

FROM Products

{unary_operator}
Is an operator that has only one numeric operand:

Unary operators can be applied only to expressions that evaluate to any of the data types of the numeric data type category.

{binary_operator}
Is an operator that defines the way two expressions are combined to yield a single result. binary _operator can be an arithmetic operator, the assignment operator (=), a bitwise operator, a comparison operator, a logical operator, the string concatenation operator (+), or a unary operator. For more information about operators, see Operators.
Expression Results

For a simple expression built of a single constant, variable, scalar function, or column name, the data type, precision, scale, and value of the expression is the data type, precision, scale, and value of the referenced element.

When two expressions are combined using comparison or logical operators, the resulting data type is Boolean and the value is one of three values: TRUE, FALSE, or UNKNOWN. For more information about Boolean data types, see Operators.

When two expressions are combined using arithmetic, bitwise, or string operators, the resulting data type is determined by the operator.

Complex expressions made up of many symbols and operators evaluate to a single-valued result. The data type, precision, and value of the resulting expression is determined by combining the component expressions two at a time until a final result is reached. The sequence in which the expressions are combined is defined by the precedence of the operators in the expression.

Remarks

Two expressions can be combined by an operator if they both have data types supported by the operator and at least one of these conditions is TRUE:

If there is no supported implicit or explicit conversion, the two expressions cannot be combined.

In a programming language such as C or Microsoft Visual Basic®, an expression always evaluates to a single result. Expressions in an Transact-SQL select list have a variation on this rule: The expression is evaluated individually for each row in the result set. A single expression may have a different value in each row of the result set, but each row has only one value for the expression. For example, in this SELECT statement both the reference to ProductID and the term 1+2 in the select list are expressions:

SELECT ProductID, 1+2

FROM Northwind.dbo.Products

  

The expression 1+2 evaluates to 3 in each row in the result set. Although the expression ProductID generates a unique value in each result set row, each row only has one value for ProductID.

See Also
CASE Functions
CAST and CONVERT LIKE
COALESCE NULLIF
Data Types SELECT
Data Type Conversion WHERE
Data Type Precedence  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.