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.
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;
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|
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;
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 ensure correct results, use parentheses where possible. The following query has the same meaning and gives the same results as the previous one, but the syntax is more precise:
SELECT Name, ( Quantity * UnitPrice ) - 5 FROM Products;
Arithmetic operations may overflow because the result of the operation can not be represented in the data type. When an overflow occurs, an error is returned instead of a value.
You can concatenate strings using a string concatenation operator. You can use either || (defined by SQL/2008) or + (supported by Adaptive Server Enterprise) as the concatenation operator. For example, the following statement retrieves and concatenates GivenName and Surname values in the results:
SELECT EmployeeID, GivenName || ' ' || Surname AS Name FROM Employees;
Although you can use operators on date and time columns, this typically involves the use of functions.
Columns can be given an alias By default the column name is the expression listed in the SELECT list, but for calculated columns the expression is cumbersome and not very informative.
Other operators are available The multiplication operator can be used to combine columns. You can use other operators, including the standard arithmetic operators, and logical operators and string operators.
For example, the following query lists the full names of all customers:
SELECT ID, (GivenName || ' ' || Surname ) AS "Full name" FROM Customers;
|| operator concatenates strings. In this query, the alias for the column has spaces, and so must be surrounded by double quotes.
This rule applies not only to column aliases, but to table names and other identifiers in the database.
Functions can be used In addition to combining columns, you can use a wide range of built-in functions to produce the results you want.
For example, the following query lists the product names in uppercase:
SELECT ID, UCASE( Name ) FROM Products;
Discuter à propos de cette page dans DocCommentXchange.
|Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1|