Back to Contents


Expressions

Summary:

See also: Variables, Data Types, Literals, Constants.


Definition

What is an Expression?

An Expression is a sequence of operands, operators, and parentheses that the runtime system can evaluate as a single value.

Expressions can include the following components:

Differences Between BDL and SQL Expressions

Expressions in SQL statements are evaluated by the database server, not by the runtime system. The set of operators that can appear in SQL expressions resembles the set of BDL operators, but they are not identical. A program can include SQL operators, but these are restricted to SQL statements. Similarly, most SQL operands are not valid in BDL expressions. The SQL identifiers of databases, tables, or columns can appear in a LIKE clause or field name in BDL statements, provided that these SQL identifiers comply with the naming rules of BDL. Here are some examples of SQL operands and operators that cannot appear in other BDL expressions:

Conversely, you cannot include BDL specific operators in SQL expressions, as for example:

Parentheses in BDL Expressions

You can use parentheses as you would in algebra to override the default order of precedence of operators. In mathematics, this use of parentheses represents the "associative" operator. It is, however, a convention in computer languages to regard this use of parentheses as delimiters rather than as operators. (Do not confuse this use of parentheses to specify operator precedence with the use of parentheses to enclose arguments in function calls or to delimit other lists.)

In the following example, the variable y is assigned the value of 2:

  LET y = 15 MOD 3 + 2

In the next example, however, y is assigned the value of 0 because the parentheses change the sequence of operations:

  LET y = 15 MOD (3 + 2)


Boolean Expressions

Boolean expression is one that evaluates to an INTEGER value that can be TRUE, FALSE and in some cases, NULL.

Usage:

Boolean expressions are a combination of Logical Operators and Boolean comparisons based on Comparison Operators. The result type of a Boolean expression is INTEGER. Any integer value different from zero is defined as true, while zero is defined as false. You can use an INTEGER or a BOOLEAN variable to store the result of a Boolean expression:
01 MAIN
02   DEFINE b BOOLEAN
03   LET b = ( TRUE AND FALSE )
04   IF b THEN
05      DISPLAY "TRUE"
06   END IF
07 END MAIN
If an expression that returns NULL is the operand of the IS NULL operator, the value of the Boolean expression is TRUE:
01 MAIN
02   DEFINE r INTEGER
03   LET r = NULL
04   IF r IS NULL THEN
05      DISPLAY "TRUE"
06   END IF
07 END MAIN
If you include a Boolean expression in a context where the runtime system expects a number, the expression is evaluated, and is then converted to an integer by the rules: TRUE = 1 and FALSE = 0:

The Boolean expression evaluates to TRUE if the value is a non-zero real number or any of the following items:

If a Boolean expression includes an operand whose value is not an integer data type, the runtime system attempts to convert the value to an integer according to the data conversion rules.

A Boolean expression evaluates to NULL if the value is NULL and the expression does not appear in any of the following contexts:

The syntax of Boolean expressions in BDL is not the same as Boolean conditions in SQL statements.

Boolean expressions in CASE, IF, or WHILE statements return FALSE if any element of the comparison is NULL, except for operands of the IS NULL and the IS NOT NULL operator. See Boolean Operators for more information about individual Boolean operators and Boolean expressions.

Example:

01 MAIN
02   DEFINE r, c INTEGER
03   LET c = 4
03   LET r = ( FALSE!=FALSE ) AND ( c=2 OR c=4 )
04   IF ( r AND canReadFile("config.txt") ) THEN
05      DISPLAY "OK"
06   END IF
07 END MAIN

Integer Expressions

An Integer expression is one that evaluates to a whole number.

Usage:

The data type of the expression result can be SMALLINT or INTEGER.

The operands must be one of:

If an integer expression includes an operand whose value is not an integer data type, the runtime system attempts to convert the value to an integer according to the data conversion rules.

If an element of an integer expression is NULL, the expression is evaluated to NULL.

Example:

01 MAIN
02   DEFINE r, c INTEGER
03   LET c = 4
04   LET r = c * ( 2 + c MOD 4 ) / getRowCount("customers")
05 END MAIN

Number Expressions

Number expression is one that evaluates to a number data type.

Usage:

The data type of the expression result can be SMALLINT, INTEGER, DECIMAL, SMALLFLOAT or FLOAT.

The operands must be one of:

If a number expression includes an operand whose value is not a numeric data type, the runtime system attempts to convert the value to a number according to the data conversion rules.

If an element of a number expression is NULL, the expression is evaluated to NULL.

Example:

01 MAIN
02   DEFINE r, c DECIMAL(10,2)
03   LET c = 456.22
04   LET r = c * 2 + ( c / 4.55 )
05 END MAIN

String Expressions

String expression is one that includes at least one character string value and that evaluates to the STRING data type.

Usage:

The data type of the expression result is STRING.

At least one of the operands must be one of:

Other operands whose values are not character string data types are converted to strings according to the data conversion rules.

If an element of an integer expression is NULL, the expression is evaluated to NULL.

An empty string ("") is equivalent to NULL.

Example:

01 MAIN
02   DEFINE r, c VARCHAR(100)
03   LET c = "abcdef" 
04   LET r = c[1,3] || ": " || TODAY USING "YYYY-MM-DD" || " " || length(c)
05 END MAIN

Date Expressions

Date expression is one that evaluates to a DATE data type.

Usage:

The data type of the expression result is a DATE value.

The operands must be one of:

If a date expression includes an operand whose value is not a date data type, the runtime system attempts to convert the value to a date value according to the data conversion rules.

If an element of an integer expression is NULL, the expression is evaluated to NULL.

Example:

01 MAIN
02   DEFINE r, c DATE
03   LET c = TODAY + 4
04   LET r = ( c - 2 )
05 END MAIN

Datetime Expressions

Datetime expression is one that evaluates to a DATETIME data type.

Usage:

The data type of the expression result is a DATETIME value.

The operands must be one of:

If a datetime expression includes an operand whose value is not a datetime data type, the runtime system attempts to convert the value to a datetime value according to the data conversion rules.

If an element of an integer expression is NULL, the expression is evaluated to NULL.

Example:

01 MAIN
02   DEFINE r, c DATETIME YEAR TO SECOND
03   LET c = CURRENT YEAR TO SECOND
04   LET r = c + INTERVAL( 234-02 ) YEAR TO MONTH
05 END MAIN

Interval Expressions

An Interval expression is one that evaluates to a INTERVAL data type.

Usage:

The data type of the expression result is a INTERVAL value.

The operands must be one of:

If an interval expression includes an operand whose value is not an interval data type, the runtime system attempts to convert the value to an interval value according to the data conversion rules.

If an element of an integer expression is NULL, the expression is evaluated to NULL.

Example:

01 MAIN
02   DEFINE r, c INTERVAL HOUR TO MINUTE
03   LET c = "12:45"
04   LET r = c + ( DATETIME(14:02) HOUR TO MINUTE - DATETIME(10:43) HOUR TO MINUTE )
05 END MAIN