Reference Manual
CHAPTER 6. SQL Language Elements
expression:
case-expression
| constant
| [correlation-name .] column-name [ java-ref ]
| - expression
| expression operator expression
| ( expression )
| function-name ( expression, ... )
| if-expression
| [ java-package-name.] java-class-name java-ref
| ( subquery )
| variable-name [ java-ref ]
case-expression:
{ CASE search-condition
... WHEN expression
... THEN,...
... [ ELSE expression ]
END
| CASE
... WHEN search-condition
... THEN expression,...
... [ ELSE expression ]
END
}
constant:
integer | number | 'string' | special-constant | host-variable
special-constant:
CURRENT { DATE | TIME | TIMESTAMP }
| NULL
| SQLCODE
| SQLSTATE
| USER
if-expression:
IF condition
... THEN expression
... [ ELSE expression ]
... ENDIF
java-ref:
.field-name [ java-ref ]
| >> field-name [ java-ref ]
| .method-name ( [expression,...] ) [ java-ref ]
| >> method-name ( [expression,...] ) [ java-ref ]
operator:
{ + | - | * | / | || | % }
Anywhere.
Must be connected to the database.
None.
Expressions are formed from several different kinds of elements, discussed in the following sections.
For information on functions, see SQL Functions. For information on variables, see SQL variables.
The IF condition is not supported in Adaptive Server Enterprise.
Java expressions are not currently supported in Adaptive Server Enterprise.
For other differences, see the separate descriptions of each class of expression, in the following sections.
Constants are numbers or strings. String constants are enclosed in apostrophes ('single quotes'). An apostrophe is represented inside the string by two apostrophes in a row.
There are several special constants:
CURRENT DATE The current year, month and day represented in the DATE data type.
CURRENT TIME The current hour, minute, second and fraction of a second represented in the TIME data type. Although the fraction of a second is stored to 6 decimal places, the current time is limited by the accuracy of the system clock.
CURRENT TIMESTAMP Combines CURRENT DATE and CURRENT TIME to form a TIMESTAMP value containing the year, month, day, hour, minute, second and fraction of a second. Like CURRENT TIME, the accuracy of the fraction of a second is limited by the system clock.
NULL The NULL value (see NULL value).
SQLCODE Current SQLCODE value (see Database Error Messages).
SQLSTATE Current SQLSTATE value (see Database Error Messages).
CURRENT USER A string containing the user ID of the current connection.
On UPDATE, columns with a default of CURRENT USER automatically update to reflect the current connection.
CURRENT PUBLISHER A string containing the publisher user ID of the database for SQL Remote replications.
LAST USER For INSERT, this constant has the same effect as CURRENT USER. On UPDATE, if a column with a default value of LAST USER is not explicitly altered, it is changed to the name of the current user. In this way, the LAST USER default indicates the user ID of the user who last modified the row.
When combined with the CURRENT TIMESTAMP, a default value of LAST USER can be used to record (in separate columns) both the user and the date and time a row was last changed.
In Embedded SQL, a host variable can also be used in an expression wherever a constant is allowed.
A column name is an identifier preceded by an optional correlation name. (A correlation name is usually a table name. For more information on correlation names, see FROM clause.) If a column name has characters other than letters, digits and underscore, it must be surrounded by quotation marks (""). For example, the following are valid column names:
employee.name address "date hired" "salary"."date paid"
See Statement elements for a complete description of identifiers.
See SQL Functions for a description of SQL functions.
A subquery is a SELECT statement enclosed in parentheses. The SELECT statement must contain one and only one select list item. Usually, the subquery is allowed to return only one row. See Search conditions for other uses of subqueries. A subquery can be used anywhere that a column name can be used. For example, a subquery can be used in the select list of another SELECT statement.
This section describes arithmetic, string, and bitwise operators. For information on comparison operators, see the section Search conditions.
The normal precedence of operations applies. Expressions in parentheses are evaluated first, then multiplication and division before addition and subtraction. String concatenation happens after addition and subtraction.
expression + expression Addition. If either expression is the NULL value, the result is the NULL value.
expression - expression Subtraction. If either expression is the NULL value, the result is the NULL value.
- expression Negation. If the expression is the NULL value, the result is the NULL value.
expression * expression Multiplication. If either expression is the NULL value, the result is the NULL value.
expression / expression Division. If either expression is the NULL value or if the second expression is 0, the result is the NULL value.
expression % expression Modulo finds the integer remainder after a division involving two whole numbers. For example, 21 % 11 = 10 because 21 divided by 11 equals 1 with a remainder of 10.
expression || expression String concatenation (two vertical bars). If either string is the NULL value, it is treated as the empty string for concatenation.
expression + expression Alternative string concatenation. When using the + concatenation operator, you must ensure the operands are explicitly set to character data types rather than relying on implicit data conversion.
SQL/92 The || operator is the SQL/92 string concatenation operator.
Sybase The + operator is supported by Adaptive Server Enterprise.
The following operators can be used on bit data types, in both Adaptive Server Anywhere and Adaptive Server Enterprise.
Operator |
Description |
---|---|
& |
and |
| |
or |
^ |
exclusive or |
~ |
not |
The Transact-SQL outer join operators *= and =* are supported in Adaptive Server Anywhere, in addition to the SQL/92 join syntax that uses a table expression in the FROM clause.
Modulo The % operator can be used in Adaptive Server Anywhere only if the PERCENT_AS_COMMENT option is set to OFF. The default value is ON.
String concatenation When using the + concatenation operator in Adaptive Server Anywhere, you should explicitly set the operands to strings rather than relying on implicit data conversion. For example, the following query returns the integer value 579
SELECT 123 + 456
whereas the following query returns the character string 123456
SELECT '123' + '456'
You can use the CAST or CONVERT function to explicitly convert data types.
The || concatenation operator is not supported by Adaptive Server Enterprise.
When you use more than one operator in an expression, it is recommended that you make the order of operation explicit using parentheses rather than relying on an identical operator precedence between Adaptive Server Enterprise and Adaptive Server Anywhere.
The syntax of the IF expression is as follows:
IF condition
THEN expression1
[ ELSE expression2 ]
ENDIF
This expression returns the following:
If condition is TRUE, the IF expression returns expression1.
If condition is FALSE, the IF expression returns expression2.
If condition is FALSE, and there is no expression2, the IF expression returns NULL.
If condition is NULL, the IF expression returns NULL.
For more information about TRUE, FALSE and UNKNOWN conditions, see NULL value, and Search conditions.
IF statement is different from IF expression For information on the IF statement, see IF statement. |
The CASE expression provides conditional SQL expressions. Case expressions can be used anywhere an expression can be used.
The syntax of the CASE expression is as follows:
CASE expression WHEN expression THEN expression,... [ ELSE expression ] END
If the expression following the CASE statement is equal to the expression following the WHEN statement, then the expression following the THEN statement is returned. Otherwise the expression following the ELSE statement is returned, if it exists.
For example, the following code uses a case expression as the second clause in a SELECT statement.
SELECT id, ( CASE name WHEN 'Tee Shirt' then 'Shirt' WHEN 'Sweatshirt' then 'Shirt' WHEN 'Baseball Cap' then 'Hat' ELSE 'Unknown' END ) as Type FROM "DBA".Product
An alternative syntax is as follows:
CASE WHEN search-condition THEN expression,... [ ELSE expression ] END
If the search-condition following the WHEN statement is satisfied, the expression following the THEN statement is returned. Otherwise the expression following the ELSE statement is returned, if it exists.
For example, the following statement uses a case expression as the third clause of a SELECT statement to associate a string with a search-condition.
SELECT id, name, ( CASE WHEN name='Tee Shirt' then 'Sale' WHEN quantity >= 50 then 'Big Sale' ELSE 'Regular price' END ) as Type FROM "DBA".Product
The NULLIF function provides a way to write some CASE statements in short form. The syntax for NULLIF is as follows:
NULLIF ( expression-1, expression-2 )
NULLIF compares the values of the two expressions. If the first expression equals the second expression, NULLIF returns NULL. If the first expression does not equal the second expression, NULLIF returns the first expression.
The following kinds of Java expressions can be used as SQL expressions:
Java fields Any field of an installed Java class can be invoked wherever an expression is required. The data type of the expression is converted from the Java field data type according to the table in Java to SQL data type conversion. Both instance fields and class fields can be used as expressions.
Java methods Any method of an installed Java class can be invoked wherever an expression is required. The data type of the expression is converted from the return type of the Java method according to the table in Java to SQL data type conversion. Both instance fields and class fields can be used as expressions.
Java objects The NEW operator is an extension to the SQL language that allows it to better assimilate Java syntax.
The NEW SQL operator performs the same operation as the new keyword in Java code: invoke a constructor method of a Java class. The data type of the NEW expression is a Java class, specifically the Java class that is being constructed.
The following expression invokes the constructor method of the String class, a member of the java.lang package.
NEW java.lang.String( 'This argument is optional' )
This expression returns a reference to the newly-created String object, which can be passed to a variable or column of type java.lang.String.
The method constructor that is being invoked determines the number and type of arguments.
The class whose constructor method is invoked must first be installed to the database.
For more information on class and instance fields and methods, see A 15 minute Java seminar.
When referencing a Java field or method from within Java code, you use the dot (.) operator. For example, to invoke the getConnection method of the DriverManager class you use the following:
conn = DriverManager.getConnection( temp.toString() , _props )
There are two ways of referencing Java fields or methods from within SQL statements. You can use either the dot operator or the >> operator.
The dot operator has the advantage that it looks like Java code, but has the disadvantage that in SQL the dot is also used to indicate the owner, table, and column hierarchy, so this could be confusing to read.
Using the dot operator, a name method of an object named Employee is invoked from SQL as follows:
select Employee.name ...
The same expression could refer to a name column of an Employee table.
The >> operator is unambiguous, but does not look like what Java programmers may expect.
The following tables describe the compatibility of expressions and constants between Adaptive Server Enterprise and Adaptive Server Anywhere. These tables are a guide only, and a marking of Both may not mean that the expression performs in an identical manner for all purposes under all circumstances. For detailed descriptions, you should refer to the Adaptive Server Enterprise documentation and the Adaptive Server Anywhere documentation on the individual expression.
In the following table, expr represents an expression, and op represents an operator.
Expression |
Supported by |
---|---|
constant |
Both |
column name |
Both |
variable name |
Both |
function (expr) |
Both |
- expr |
Both |
expr op expr |
Both |
( expr ) |
Both |
( subquery ) |
Both |
if-expression |
Adaptive Server Anywhere only |
Constant |
Supported by |
---|---|
integer |
Both |
number |
Both |
'string' |
Both |
special-constant |
Both |
host-variable |
Adaptive Server Anywhere |
By default, Adaptive Server Enterprise and Adaptive Server Anywhere give different meanings to delimited strings: that is, strings enclosed in apostrophes (single quotes) and in quotation marks (double quotes).
Adaptive Server Anywhere employs the SQL/92 convention, that strings enclosed in apostrophes are constant expressions, and strings enclosed in quotation marks (double quotes) are delimited identifiers (names for database objects). Adaptive Server Enterprise employs the convention that strings enclosed in quotation marks are constants, while delimited identifiers are not allowed by default and are treated as strings.
Both Adaptive Server Enterprise and Adaptive Server Anywhere provide a quoted_identifier option that allows the interpretation of delimited strings to be changed. By default, the quoted_identifier option is set to OFF in Adaptive Server Enterprise, and to ON in Adaptive Server Anywhere.
You cannot use SQL reserved words as identifiers if the quoted_identifier option is off.
For a complete list of reserved words, see Alphabetical list of keywords.
While the Transact-SQL SET statement is not supported for most Adaptive Server Enterprise connection options, it is supported for the quoted_identifier option.
The following statement in either Adaptive Server Anywhere or Adaptive Server Enterprise changes the setting of the quoted_identifier option to ON:
SET quoted_identifier ON
With the quoted_identifier option set to ON, Adaptive Server Enterprise allows table, view, and column names to be delimited by quotes. Other object names cannot be delimited in Adaptive Server Enterprise.
The following statement in Adaptive Server Anywhere or Adaptive Server Enterprise changes the setting of the quoted_identifier option to OFF:
SET quoted_identifier OFF
You can choose to use either the SQL/92 or the default Transact-SQL convention in both Adaptive Server Enterprise and Adaptive Server Anywhere as long as the quoted_identifier option is set to the same value in each DBMS.
If you choose to operate with the quoted_identifier option on (the default Adaptive Server Anywhere setting), then the following statements involving the SQL keyword user are valid for both DBMS's.
CREATE TABLE "user" ( col1 char(5) ) ; INSERT "user" ( col1 ) VALUES ( 'abcde' ) ;
If you choose to operate with the quoted_identifier option off (the default Adaptive Server Enterprise setting), then the following statements are valid for both DBMSs.
SELECT * FROM employee WHERE emp_lname = "Chin"