Expressions (Database Engine)

An expression is a combination of identifiers, values, and operators that SQL Server can evaluate to obtain a result. The data can be used in several different places when you are accessing or changing data. Expressions can be used, for example, as part of the data to retrieve in a query, or as a search condition when looking for data that meets a set of criteria.

An expression can be any of the following:

  • Constant

  • Function

  • Column name

  • Variable

  • Subquery


An expression can also be built from combinations of these entities joined by operators.

In the following SELECT statement, for each row of the result set, SQL Server can resolve LastName to a single value. Therefore, it is an expression.

USE AdventureWorks;
SELECT LastName 
FROM AdventureWorks.Person.Contact;

An expression can also be a calculation such as (price * 1.5) or (price + sales_tax).

In an expression, enclose character and datetime values in single quotation marks. In the following SELECT statement, the character literal B% that is used as the pattern for the LIKE clause must be in single quotation marks:

USE AdventureWorks;
SELECT LastName, FirstName 
FROM AdventureWorks.Person.Contact 
WHERE LastName LIKE 'Bai%';

In the following SELECT statement, the date value is enclosed in quotation marks.

USE AdventureWorks;
SELECT c.FirstName, c.LastName, e.HireDate 
FROM Person.Contact c JOIN HumanResources.Employee e 
ON c.ContactID = e.EmployeeID
WHERE e.HireDate = 'July 1, 2003';

In the following example, more than one expression is used in the query. For example, Name, SUBSTRING, ProductNumber, ListPrice, and 1.5 are all expressions.

USE AdventureWorks;
   SUBSTRING('This is a long string', 1, 5) AS SampleText, 
   ListPrice * 1.5 AS NewPrice
FROM Production.Product;