Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » Queries: Selecting Data from a Table » The SELECT list: specifying columns

Character strings in query results Next Page

Computing values in the SELECT list


The expressions in the select list can be more complicated than just column names or strings. For example, you can perform computations with data from numeric columns in a select list.

Arithmetic operations

To illustrate the numeric operations you can perform in the select list, you start with a listing of the names, quantity in stock, and unit price of products in the SQL Anywhere sample database.

SELECT Name, Quantity, UnitPrice
FROM Products;
Name Quantity UnitPrice
Tee Shirt 28 9
Tee Shirt 54 14
Tee Shirt 75 14
Baseball Cap 112 9
... ... ...

Suppose the practice is to replenish the stock of a product when there are ten items left in stock. The following query lists the number of each product that must be sold before re-ordering:

SELECT Name, Quantity - 10
   AS "Sell before reorder"
FROM Products;
Name Sell before reorder
Tee Shirt 18
Tee Shirt 44
Tee Shirt 65
Baseball Cap 102
... ...

You can also combine the values in columns. The following query lists the total value of each product in stock:

SELECT Name,
   Quantity * UnitPrice AS "Inventory value"
FROM Products;
Name Inventory value
Tee Shirt 252.00
Tee Shirt 756.00
Tee Shirt 1050.00
Baseball Cap 1008.00
... ...
Arithmetic operator precedence

When there is more than one arithmetic operator in an expression, multiplication, division, and modulo are calculated first, followed by subtraction and addition. When all arithmetic operators in an expression have the same level of precedence, the order of execution is left to right. Expressions within parentheses take precedence over all other operations.

For example, the following SELECT statement calculates the total value of each product in inventory, and then subtracts five dollars from that value.

SELECT Name, Quantity * UnitPrice - 5
FROM Products;

To avoid misunderstandings, it is recommended that you use parentheses. The following query has the same meaning and gives the same results as the previous one, but some may find it easier to understand:

SELECT Name, ( Quantity * UnitPrice ) - 5
FROM Products;

For more information on operator precedence, see Operator precedence.

String operations

You can concatenate strings using a string concatenation operator. You can use either || (SQL/2003 compliant) or + (supported by Adaptive Server Enterprise) as the concatenation operator.

The following example illustrates the use of the string concatenation operator in the select list:

SELECT EmployeeID, GivenName || ' ' || Surname AS Name
FROM Employees;
EmployeeID Name
102 Fran Whitney
105 Matthew Cobb
129 Philip Chin
148 Julie Jordan
... ...
Date and time operations

Although you can use operators on date and time columns, this typically involves the use of functions. For information on SQL functions, see SQL Functions.

Additional notes on calculated columns