Expressions

 

The expressions specified on the Open Query File (OPNQRYF) command must follow these conventions.

Expressions specified on the QRYSLT, GRPSLT, and MAPFLD parameters are similar to expressions specified on other control language (CL) command parameters. Logical, relational, numeric, and string operations are performed by using combinations of field values and constants. Symbolic and named operators are supported, as well as many built-in functions, and parentheses are used to control the order of evaluation. There are also differences in the expressions specified on OPNQRYF parameters and on other CL command parameters. Listed here are the ways that expressions on the QRYSLT, GRPSLT, and MAPFLD parameters differ from normal CL expressions:

The following table shows the priority of all operators that are used for expressions on the QRYSLT, GRPSLT, or MAPFLD parameters. Only operators listed for priorities 1 through 5, excluding the *NOT and operators, are allowed in an expression specified on the MAPFLD parameter:

Priority Operators
1 +, - (when used for signed numeric values), *NOT, ¬
2 **
3 *, / ,// (a / must have a space before the / and/or after the /)
4 +, - (when used between two operands)
5 *CAT, | |
6 *GT, *LT, *EQ, *GE, *LE, *NE, *NG, *NL, *CT, >, <, =, >=, <=, ¬=, ¬>, ¬<
7 *AND, &
8 *OR, *XOR, |, &&

Except for operators ¬ and *NOT, the operators for priorities 1 through 4 are numeric operators, which require numeric operands. The operators for priority 5 are string operators, which require operands to be either character or DBCS strings. Priority 6 operators are called relational operators, which require at least one operand that is a field name or a numeric or string expression (not a constant). The operators for priorities 7 and 8, plus the ¬ and *NOT operators (priority 1), are logical operators. The operands in a logical expression are relations (constructed by using a relational operator with appropriate operands) and other logical expressions.

The operands in a string expression, including string operands for a built-in function, are a combination of character fields and DBCS fields and constants. If both operands of such an expression are DBCS-only fields or constants, the final result from evaluation of the expression is a DBCS-only field value. If the operands are a combination of DBCS or character fields or constants, the result is a DBCS-open field value. When DBCS fields are concatenated, the extraneous shift-in and shift-out characters between the fields are removed.

The result produced by a + or - sign prefixed operator has the same attributes as the operand, unless the operand of a - sign prefixed operator is a *BIN2, in which case the result is a *BIN4. The result of an ** operator (exponentiation) is a double-precision floating-point number (*FLT8). For other numeric operators that require two operands, if either operand is a floating-point number, the result is a double-precision floating point number (*FLT8). If both operands are fixed-point numbers, the system uses the information in the following table to determine the number of total and fractional digits required to produce a packed decimal (*DEC) result. If both operands are zero-precision binary fields and/or integer constants, the result is a *BIN4, unless the operator is a "/". In that case, the result is the same as for a fixed-point result. If the total number of digits required exceeds 31, the number of fraction digits is reduced enough to enable calculation of the result with a total of 31 digits. If some fraction digits are dropped and the attributes of the end result of the computation (the attributes specified on the MAPFLD parameter for the field) require greater precision than that of the intermediate result, a warning message is sent to indicate that some precision was lost in evaluating the expression.

Operation Result (Total Digits) Result (Fractional Digits)
+ MAX(d1-f1, d2-f2)+MAX(f1,f2)+1 MAX(f1, f2)
_ MAX (d1-f1, d2-f2)+MAX (f1,f2)+1 MAX(f1, f2)
* d1+d2 f1+f2
/ 31 31-(d1-f1+f2)
// MIN(d1-f1,d2-f2)+MAX(f1,f2) MAX(f1,f2)
Legend:

d1

Total digits in operand 1

f1

Fractional digits in operand 1

d2

Total digits in operand 2

f2

Fractional digits in operand 2

When a numeric or string expression is specified on the MAPFLD parameter, the attributes of the final result are used in one of the two ways. They are either used directly for the field value (if field-type *CALC is specified and the field is not contained in the prototype record format identified on the FORMAT parameter), or the final result is changed to match the attributes specified on the MAPFLD parameter or contained in the field definition in the record format identified by the FORMAT parameter.

Both operands of a relational operator can be constants. The fields, constants, or expressions specified as operands on the left and right side of a relational operator must be of the same type, either numeric or string. Any combination of character and DBCS field operands are allowed except that a character field cannot be related to a DBCS-only field.

There are two types of DBCS constants: DBCS-only and DBCS-open. A DBCS-only constant has only DBCS data between its single quotation marks. This data must be enclosed in SO/SI characters. A DBCS-open constant has a mixture of DBCS and alphameric data. An SO character (HEX 0E) indicates the start of a group of DBCS characters and an SI character (HEX 0F) follows the last double-byte character of the group.

If a numeric or string expression appears as a complex selection operand on the QRYSLT or GRPSLT parameter, the attributes of the final result of the expression used for the selection operand are changed to match the other relational operand.

It is not necessary for operands of a relational operator to have identical attributes, but numeric operands cannot be mixed with character operands. If the operands do not have identical attributes, the system changes them to identical attributes (except for the *CT operator, where the character string operands might be of different lengths), before performing the operation. This change uses packed decimal format if both operands are fixed-point numeric operands, or floating-point format if either operand is a floating-point number. The changes for fixed-point numeric operands align their decimal points and pad them with zeros. Numeric type changes might truncate fractional digits if more than 31 total digits are required for fixed-point numbers, or might drop some of the least significant digits if more than 15 total digits are required for floating-point numbers. Character operands are changed by padding the shorter operand with blanks.

The *CT operator performs a scan of the character field or string expression (except for expressions made up of a single character string literal) that must be specified as the left side of the relation, in order to determine if it contains the character string, field, or expression value specified as the right side of the relation. The second operand (the search value) must be no longer than the first operand (the base string). If the string is found, the relation is satisfied and the result is a logical value of 'true'; otherwise, the result is a logical 'false' value. The following example illustrates this process:

Expression Result
'BASEFLD *CT ''IS A''' True
'BASEFLD *CT TESTFLD True
'BASEFLD *CT ''X''' False
'BASEFLD *CT TESTFLD | | ''Z''' False
'BASEFLD | | ''ABC'' *CT ''TAB''' True

 

Parent topic:

Using Open Query File (OPNQRYF) command