Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   Data Types and Expressions >

Expression

Data values in SQL statements must be represented as expressions. An expression is a specification, which can include operators, operands, and parentheses, that the database server can evaluate to one or more values, or to a reference to some database object.

Expressions can refer to values already in a table of the database, or to values derived from such data, but some expressions (such as TODAY, USER, or literal values) can return values that are independent of the database. You can use expressions to specify values in data-manipulation statements, to define fragmentation strategies, and in other contexts. Use the Expression segment whenever you see a reference to an expression in a syntax diagram.

In most contexts, however, you are restricted to expressions whose returned value is of some specific data type, or of a data type that can be converted by the database server to some required data type.

For an alphabetical listing of the built-in operators and functions that are described in this segment, see List of Expressions.

Syntax

The sections that follow describe SQL expressions, which are specifications that return one or more values or references to database objects. IBM Informix database servers support the following categories of expressions:

Read syntax diagramSkip visual syntax diagramSQL Expressions:
 
   .-Binary Operators------------------------------------------.
   V                                (1)                        |
|----+----+--+-| Cast Expressions |--------------------------+-+--|
     +- --+  |                        (2)                    |
     '- +-'  +-| Column Expressions |------------------------+
             |                             (3)               |
             +-| Conditional Expressions |-------------------+
             |                          (4)                  |
             +-| Constant Expressions |----------------------+
             |                             (5)               |
             +-| Constructor Expressions |-------------------+
             |                          (6)                  |
             +-| Function Expressions |----------------------+
             |                                          (7)  |
             +-| Statement-Local Variable Expressions |------+
             |                           (8)                 |
             +-| Aggregate Expressions |---------------------+
             +-NULL------------------------------------------+
             +-variable--------------------------------------+
             |  (9)                                          |
             +--------SPL_variable---------------------------+
             '-(-| Expression |-)----------------------------'
 
Binary Operators:
 
|--+- +--+------------------------------------------------------|
   +- ---+
   +- *--+
   +- /--+
   '- ||-'
 

Notes:
  1. See page Cast Expressions (IDS)
  2. See page Column Expressions
  3. See page Conditional Expressions
  4. See page Constant Expressions
  5. See page Constructor Expressions (IDS)
  6. See page Function Expressions
  7. See page Statement-Local Variable Expressions (IDS)
  8. See page Aggregate Expressions
  9. Stored Procedure Language only

Element Description Restrictions Syntax
SPL_variable In an SPL routine, a variable that contains some expression type that the syntax diagram shows Must conform to the rules for expressions of that type Identifier, p. Identifier
variable Host or program variable that contains some expression type that the syntax diagram shows Must conform to the rules for expressions of that type Language-specific rules for names

Usage

The following table lists the types of SQL expressions, as identified in the diagram for Expression, and describes what each type returns.

Expression Type Description
Aggregate functions Returns values from built-in or from user-defined aggregates
Arithmetic operators Supports arithmetic operations on one (unary operators) or two (binary operators) numeric operands
Concatenation operator Concatenates two string values
Cast operators Explicit casts from one data type to another
Column expressions Full or partial column values
Conditional expressions Returns values that depend on conditional tests
Constant expressions Literal values in data manipulation (DML) statements
Constructor expressions Dynamically creates values for complex data types
Function expressions Returns values from built-in or user-defined functions
Statement-Local-Variable expressions Specifies how you can use a defined statement-local variable (SLV) elsewhere in an SQL statement

You can also use host variables or SPL variables as expressions. For a complete list with page references to this chapter, see the following "List of Expressions."

List of Expressions

Each category of SQL expression includes many individual expressions. The following table lists all the SQL expressions (and some operators) in alphabetical order. The columns in this table have the following meanings:

Each expression listed in the following table is supported on all database servers unless otherwise noted. When an expression is not supported on all database servers, the Name column notes in parentheses the database server or servers that do support the expression.

Name Description Syntax Usage
ABS function Returns absolute value of a numeric argument p. Algebraic Functions p. ABS Function
ACOS function Returns the arc cosine of a numeric argument p. Trigonometric Functions p. ACOS Function
Addition ( + ) operator Returns the sum of two numeric operands p. Syntax p. Arithmetic Operators
ASIN function Returns the arc sine of a numeric argument p. Trigonometric Functions p. ASIN Function
ATAN function Returns the arc tangent of numeric argument p. Trigonometric Functions p. ATAN Function
ATAN2 function Computes the angular component of the polar coordinates (r, q) associated with (x, y) p. Trigonometric Functions p. ATAN2 Function
AVG function Returns the mean of a set of numeric values p. Aggregate Expressions p. AVG Function
CARDINALITY function (IDS) Returns the number of elements in a collection data type (SET, MULTISET, or LIST) p. CARDINALITY Function (IDS) p. CARDINALITY Function (IDS)
CASE expression Returns a value that depends on which of several conditional tests evaluates to true p. CASE Expressions p. CASE Expressions
CAST expression (IDS) Converts an expression to a specified data type p. Cast Expressions (IDS) p. Cast Expressions (IDS)
Cast ( :: ) operator See "Double-colon ( :: ) cast operator" p. Cast Expressions (IDS) p. Cast Expressions (IDS)
CHARACTER_LENGTH function See CHAR_LENGTH function. (In multibyte locales, this replaces the LENGTH function.) p. Length Functions p. The CHAR_LENGTH Function
CHAR_LENGTH function Returns count of logical characters in a string p. Length Functions p. The CHAR_LENGTH Function
Column expression Complete or partial column value from a table p. Column Expressions p. Column Expressions
Concatenation ( || ) operator Concatenates the results of two expressions p. Syntax p. Concatenation Operator
Constant expression Expression with a literal, fixed, or variant value p. Constant Expressions p. Constant Expressions
COS function Returns the cosine of a radian expression p. Trigonometric Functions p. COS Function
COUNT (as a set of functions) Functions that return frequency counts Each form of the COUNT function is listed below. p. Aggregate Expressions p. Overview of COUNT Functions
COUNT (ALL column) function See COUNT (column) function. p. Aggregate Expressions p. COUNT column Function
COUNT (column) function Returns the number of non-NULL values in a specified column p. Aggregate Expressions p. COUNT column Function
COUNT DISTINCT function Returns the number of unique non-NULL values in a specified column p. Aggregate Expressions p. COUNT DISTINCT and COUNT UNIQUE Functions
COUNT UNIQUE function See COUNT DISTINCT function. p. Aggregate Expressions p. COUNT DISTINCT and COUNT UNIQUE Functions
COUNT (*) function Returns the cardinality of the set of rows that satisfy a query p. Aggregate Expressions p. COUNT(*) Function
CURRENT operator Returns the current time as a DATETIME value that consists of the date and the time of day p. Constant Expressions p. CURRENT Operator
CURRENT_ROLE operator Returns the currently enabled role of the user p. Constant Expressions p. CURRENT_ROLE Operator
sequence.CURRVAL (IDS) Returns the current value of specified sequence p. *** p. Using CURRVAL
DATE function Converts a nondate argument to a DATE value p. Time Functions p. DATE Function
DAY function Returns the day of the month as an integer p. Time Functions p. DAY Function
DBINFO (as a set of functions) Provides a set of functions for retrieving different types of database information. To invoke each function, specify the appropriate DBINFO option. Each option is listed below. p. DBINFO Function p. DBINFO Options
DBINFO ('coserverid' string followed by table. column and the 'currentrow' string) (XPS) Returns the coserver ID of the coserver where each row of a specified table is located p. DBINFO Function p. Using the 'coserverid' Option Followed by Table and Column Names (XPS)
DBINFO ('coserverid' string with no other arguments) (XPS) Returns the coserver ID of the coserver to which the user who entered the query is connected p. DBINFO Function p. Using the 'coserverid' Option with No Other Arguments (XPS)
DBINFO ('dbhostname' option) Returns the hostname of the database server to which a client application is connected p. DBINFO Function p. Using the 'dbhostname' Option
DBINFO ('dbspace' string followed by table.column and the 'currentrow' string) (XPS) Returns the name of the dbspace where each row of a specified table is located p. DBINFO Function p. Using the 'dbspace' Option Followed by Table and Column Names (XPS)
DBINFO ('dbspace' string followed by a tblspace number) Returns the name of a dbspace corresponding to a tblspace number p. DBINFO Function p. Using the 'dbspace' Option Followed by a Tblspace Number
DBINFO ('serial8' option) Returns most recently inserted SERIAL8 value p. DBINFO Function p. Using the 'serial8' Option
DBINFO ('sessionid' option) Returns the session ID of the current session p. DBINFO Function p. Using the 'sessionid' Option
DBINFO ('sqlca.sqlerrd1' option) Returns the last serial value inserted in a table p. DBINFO Function p. Using the 'sqlca.sqlerrd1' Option
DBINFO ('sqlca.sqlerrd2' option) Returns the number of rows processed by DML statements, and by EXECUTE PROCEDURE and EXECUTE FUNCTION statements p. DBINFO Function p. Using the 'sqlca.sqlerrd2' Option
DBINFO ('version' option) Returns exact version of the database server to which a client application is connected p. DBINFO Function p. Using the 'version' Option
DBSERVERNAME function Returns the name of the database server p. Constant Expressions p. DBSERVERNAME and SITENAME Operators
DECODE function Evaluates one or more expression pairs and compares the when expression in each pair with a specified value expression p. DECODE Function p. DECODE Function
DECRYPT_CHAR function (IDS) Returns a plain-text string or CLOB after processing an encrypted argument p. Encryption and Decryption Functions p. ***
DECRYPT_BINARY function (IDS) Returns a plain-text BLOB data value after processing an encrypted BLOB argument p. Encryption and Decryption Functions p. ***
DEFAULT_ROLE operator Returns the default role of the current user p. Constant Expressions p. DEFAULT_ROLE Operator
Division ( / ) operator Returns the quotient of two numeric operands p. Syntax p. Arithmetic Operators
Double-colon ( :: ) cast operator (IDS) Converts the value of an expression to a specified data type p. Cast Expressions (IDS) p. Cast Expressions (IDS)
Double-pipe ( || ) concatenation operator Returns a string that joins one string operand to another string operand p. Syntax p. Concatenation Operator
ENCRYPT_AES function (IDS) Returns an encrypted string or BLOB after processing a plain-text string, BLOB, or CLOB p. Encryption and Decryption Functions p. ***
ENCRYPT_TDES function (IDS) Returns an encrypted string or BLOB after processing a plain-text string, BLOB, or CLOB p. Encryption and Decryption Functions p. ENCRYPT_TDES Function
EXP function Returns the exponent of a numeric expression p. Exponential and Logarithmic Functions p. EXP Function
EXTEND function Resets precision of DATETIME or DATE value p. Time Functions p. EXTEND Function
FILETOBLOB function (IDS) Creates a BLOB value from data stored in a specified operating-system file p. Smart-Large-Object Functions (IDS) p. FILETOBLOB and FILETOCLOB Functions
FILETOCLOB function (IDS) Creates a CLOB value from data stored in a specified operating-system file p. Smart-Large-Object Functions (IDS) p. FILETOBLOB and FILETOCLOB Functions
GETHINT function (IDS) Returns a plain-text hint string after processing an encrypted data-string argument p. Encryption and Decryption Functions p. GETHINT Function
HEX function Returns the hexadecimal encoding of a base-10 integer argument p. HEX Function p. HEX Function
Host variable See Variable. p. Syntax p. Syntax
IFX_ALLOW_NEWLINE function Sets a newline session mode that allows or disallows newline characters in quoted strings p. IFX_ALLOW_NEWLINE Function p. IFX_ALLOW_NEWLINE Function
IFX_REPLACE_MODULE function (IDS) Replaces a loaded shared-object file with a new version that has a different name or location p. IFX_REPLACE_MODULE Function (IDS, C) p. IFX_REPLACE_MODULE Function (IDS, C)
INITCAP function Converts a string argument to a string in which only the initial letter of each word is uppercase p. Case-Conversion Functions p. INITCAP Function
LENGTH function Returns the number of bytes in a character column, not including any trailing blank spaces p. Length Functions p. The LENGTH Function
LIST collection constructor (IDS) Constructor for collections whose elements are ordered and can contain duplicate values p. Collection Constructors p. Collection Constructors
Literal BOOLEAN Literal representation of a BOOLEAN value p. Constant Expressions p. Constant Expressions
Literal collection (IDS) Represents elements in a collection data type p. Constant Expressions p. Literal Collection (IDS)
Literal DATETIME Represents a DATETIME value p. Constant Expressions p. Literal DATETIME
Literal INTERVAL Represents an INTERVAL value p. Constant Expressions p. Literal INTERVAL
Literal number Represents a numeric value p. Constant Expressions p. Literal Number
Literal opaque type (IDS) Represents an opaque data type p. Constant Expressions p. Constant Expressions
Literal row (IDS) Represents the elements in a ROW data type p. Constant Expressions p. Literal Row (IDS)
LOCOPY function (IDS) Creates a copy of a smart large object p. Smart-Large-Object Functions (IDS) p. LOCOPY Function
LOGN function Returns the natural log of a numeric argument p. Exponential and Logarithmic Functions p. LOGN Function
LOG10 function Returns the base-10 logarithm of an argument p. Exponential and Logarithmic Functions p. LOG10 Function
LOTOFILE function (IDS) Copies a BLOB or CLOB value to a file p. Smart-Large-Object Functions (IDS) p. LOTOFILE Function
LOWER function Converts uppercase letters to lowercase p. Case-Conversion Functions p. LOWER Function
LPAD function Returns a string that is left-padded by a specified number of pad characters p. LPAD Function p. LPAD Function
MAX function Returns the largest in a specified set of values p. Aggregate Expressions p. MAX Function
MDY function Returns a DATE value from integer arguments p. Time Functions p. MDY Function
MIN function Returns the smallest in a specified set of values p. Aggregate Expressions p. MIN Function
MOD function Returns the modulus (the integer-division remainder value) from two numeric arguments p. Algebraic Functions p. MOD Function
MONTH function Returns the month value from a DATE or DATETIME argument p. Time Functions p. MONTH Function
Multiplication ( * ) operator Returns the product of two numeric operands p. Syntax p. Arithmetic Operators
MULTISET collection constructor (IDS) Constructor for a non-ordered collection of elements that can contain duplicate value p. Collection Constructors p. Collection Constructors
sequence.NEXTVAL (IDS) Increments the value of the specified sequence p. *** p. Using NEXTVAL
NULL keyword Unknown, missing, or logically undefined value p. NULL Keyword p. ***
NVL function Returns the value of a not-NULL argument, or a specified value if the argument is NULL p. NVL Function p. NVL Function
OCTET_LENGTH function Returns the number of bytes in a character column, including any trailing blank spaces p. Length Functions p. The OCTET_LENGTH Function
POW function Raises a base value to a specified power p. Algebraic Functions p. POW Function
Procedure-call expression See user-defined function. p. User-Defined Functions p. User-Defined Functions
Program variable See variable. p. Syntax p. Syntax
Quoted string Literal character string p. Constant Expressions p. Quoted String
RANGE function Returns the range of a specified set of values p. Aggregate Expressions p. RANGE Function
REPLACE function Replaces specified characters in a source string p. REPLACE Function p. REPLACE Function
ROOT function Returns the root value of a numeric argument p. Algebraic Functions p. ROOT Function
ROUND function Returns the rounded value of an argument p. Algebraic Functions p. ROUND Function
ROW constructor (IDS) Constructor for a named ROW data type p. Constructor Expressions (IDS) p. ROW Constructors
RPAD function Returns a string that is right-padded by a specified number of pad characters p. RPAD Function p. RPAD Function
SET collection constructor (IDS) Constructor for an unordered collection of elements in which each value is unique p. Collection Constructors p. Collection Constructors
SIN function Returns the sine of a radian expression p. Trigonometric Functions p. SIN Function
SITENAME function See DBSERVERNAME function. p. Constant Expressions p. DBSERVERNAME and SITENAME Operators
SPL routine expression See "User-defined functions" p. User-Defined Functions p. User-Defined Functions
SPL variable SPL variable that stores an expression p. Syntax p. Syntax
SQRT function Returns the square root of a numeric argument p. Algebraic Functions p. SQRT Function
Statement-Local-Variable expression A statement-local variable (SLV) whose scope is restricted to the same SQL statement p. Statement-Local Variable Declaration (IDS) p. Statement-Local Variable Expressions (IDS)
STDEV function Returns the standard deviation of a data set p. Aggregate Expressions p. STDEV Function
SUBSTR function Returns a substring of a string argument p. SUBSTR Function p. SUBSTR Function
SUBSTRING function Returns a substring of a source string p. SUBSTRING Function p. SUBSTRING Function
Substring ( [ x, y ] ) operator Returns a substring of a string operand p. Column Expressions p. Relational-Operator Condition
Subtraction ( - ) operator Returns the difference between two numbers p. Syntax p. Arithmetic Operators
SUM function Returns the sum of a specified set of values p. Aggregate Expressions p. SUM Function
TAN function Returns the tangent of a radian expression p. Trigonometric Functions p. TAN Function
TO_CHAR function Converts a DATE or DATETIME to a string p. Time Functions p. TO_CHAR Function (IDS)
TO_DATE function Converts a string to a DATETIME value p. Time Functions p. TO_DATE Function (IDS)
TODAY operator Returns the current system date p. Constant Expressions p. TODAY Operator
TRIM function Drops pad characters from a string argument p. TRIM Function p. TRIM Function
TRUNC function Returns a truncated numeric value p. Algebraic Functions p. TRUNC Function
Unary minus ( - ) sign Specifies a negative ( < 0 ) numeric value p. Syntax p. Arithmetic Operators
Unary plus ( + ) sign Specifies a positive ( > 0 ) numeric value . p. Syntax p. Arithmetic Operators
UNITS operator Convert an integer to an INTERVAL value p. Constant Expressions p. UNITS Operator
UPPER function Converts lowercase letters to uppercase p. Case-Conversion Functions p. UPPER Function
User-defined aggregate (IDS) Aggregate that you define (as opposed to built-in aggregates that Dynamic Server provides) p. User-Defined Aggregates (IDS) p. User-Defined Aggregates (IDS)
User-defined function Function that you write (as opposed to built-in functions that the database server provides) p. User-Defined Functions p. User-Defined Functions
USER operator Returns the login name of the current user p. Constant Expressions p. USER Operator
Variable Host or program variable that stores a value p. Syntax p. Syntax
VARIANCE function Returns the variance for a set of values p. Aggregate Expressions p. VARIANCE Function
WEEKDAY function Returns an integer code for the day of the week p. Time Functions p. WEEKDAY Function
YEAR function Returns a 4-digit integer representing a year p. Time Functions p. YEAR Function
* symbol See "Multiplication ( * ) operator" p. Syntax p. Arithmetic Operators
+ symbol See "Addition" and "Unary plus ( + ) sign" p. Syntax p. Arithmetic Operators
- symbol See "Subtraction" and "Unary minus ( - ) sign" p. Syntax p. Arithmetic Operators
/ symbol See "Division operator" p. Syntax p. Arithmetic Operators
:: symbols See "Double-colon ( :: ) cast operator" p. Cast Expressions (IDS) p. Cast Expressions (IDS)
|| symbol See "Double-pipe ( || ) concatenation operator" p. Syntax p. Concatenation Operator
[ first, last ] symbols See "Substring operator" p. Syntax p. Arithmetic Operators

Sections that follow describe the syntax and usage of each expression that appears in the preceding table.

Arithmetic Operators

Binary arithmetic operators can combine expressions that return numbers.

Arithmetic
Operation
Arithmetic
Operator
Operator
Function
Arithmetic
Operation
Arithmetic
Operator
Operator
Function
Addition + plus( ) Multiplication * times( )
Subtraction minus( ) Division / divide( )

The following examples use binary arithmetic operators:

quantity * total_price 
price * 2 
COUNT(*) + 2 

If you combine a DATETIME value with one or more INTERVAL values, all the fields of the INTERVAL value must be present in the DATETIME value; no implicit EXTEND function is performed. In addition, you cannot use YEAR to MONTH intervals with DAY to SECOND intervals. For additional information about binary arithmetic operators, see the IBM Informix Guide to SQL: Reference.

The binary arithmetic operators have associated operator functions, as the preceding table shows. Connecting two expressions with a binary operator is equivalent to invoking the associated operator function on the expressions. For example, the following two statements both select the product of the total_price column and 2. In the first statement, the * operator implicitly invokes the times( ) function.

SELECT (total_price * 2) FROM items 
   WHERE order_num = 1001
SELECT times(total_price, 2) FROM items
   WHERE order_num = 1001

You cannot use arithmetic operators to combine expressions that use aggregate functions with column expressions.

The database server provides the operator functions associated with the relational operators for all built-in data types. You can define new versions of these operator functions to handle your own user-defined data types.

For more information, see IBM Informix User-Defined Routines and Data Types Developer's Guide.

The database server also supports the following unary arithmetic operators.

Sign of Number Unary Arithmetic Operator Operator Function
Positive + positive( )
Negative negate( )

The unary arithmetic operators have the associated operator functions that the preceding table shows. You can define new versions of these functions to handle your own user-defined data types. For more information on this topic, see IBM Informix User-Defined Routines and Data Types Developer's Guide.

If any value that participates in an arithmetic expression is NULL, the value of the entire expression is NULL, as the following example shows:

SELECT order_num, ship_charge/ship_weight FROM orders
   WHERE order_num = 1023

If either ship_charge or ship_weight is NULL, the value returned for the expression ship_charge/ship_weight is also NULL. If the NULL expression ship_charge/ship_weight is used in a condition, its truth value cannot be TRUE, and the condition is not satisfied (unless the NULL expression is an operand of the IS NULL operator).

Concatenation Operator

You can use the concatenation operator ( || ) to concatenate two expressions. These examples show some possible concatenated-expression combinations.

You cannot use the concatenation operator in an embedded-language-only statement. The ESQL/C-only statements of SQL appear in the following list:

ALLOCATE COLLECTION
ALLOCATE DESCRIPTOR
ALLOCATE ROW
CLOSE
CREATE FUNCTION FROM
CREATE PROCEDURE FROM
CREATE ROUTINE FROM
DEALLOCATE COLLECTION
DEALLOCATE DESCRIPTOR
DEALLOCATE ROW
DECLARE
DESCRIBE
DESCRIBE INPUT
EXECUTE

EXECUTE IMMEDIATE
FETCH
FLUSH
FREE
GET DESCRIPTOR
GET DIAGNOSTICS
OPEN
PREPARE
PUT
SET AUTOFREE
SET CONNECTION
SET DESCRIPTOR
WHENEVER

You can use the concatenation operator in a SELECT, INSERT, EXECUTE FUNCTION, or EXECUTE PROCEDURE statement within the DECLARE statement.

You can use the concatenation operator in the SQL statement or statements in the PREPARE statement.

The concatenation operator ( || ) has an associated operator function called concat( ). You can define a concat( ) function to handle your own string-based user-defined data types. For more information, see IBM Informix User-Defined Routines and Data Types Developer's Guide.

Cast Expressions (IDS)

You can use the CAST AS keywords or the double-colon cast operator ( :: ) to cast an expression to another data type. Both the operator and the keywords invoke a cast from the data type of the expression to the target data type.

To invoke an explicit cast, you can use either the cast operator or the CAST AS keywords. If you use the cast operator or the CAST AS keywords, but no explicit or implicit cast was defined to perform the conversion between two data types, the statement returns an error.

Read syntax diagramSkip visual syntax diagramCast Expressions:
 
                             (1)
|--+-CAST--(--| Expression |-------+--------------------------+--AS--target_data_type--)-+--|
   |                               | .----------------------. |                          |
   |                               | V                      | |                          |
   |                               '---::--target_data_type-+-'                          |
   |                      .----------------------.                                       |
   |                (1)   V                      |                                       |
   '-| Expression |---------::--target_data_type-+---------------------------------------'
 
Notes:
  1. See page Expression
Element Description Restrictions Syntax
target_data_type Data type returned by cast See "Rules for the Target Data Type" Data Type, p. Data Type
Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]