For more information about those cases, including assigning a reference variable to a value variable (and the other way around), see Reference compatibility in EGL.
In the following tables, the types in the leftmost column represent the left hand side (the target) of the assignment statement, and all other columns represent the right hand side (source). The letter Y indicates compatible types. A blank means the types are not compatible. A number refers to notes that follow the third table, detailing the circumstances under which the types are compatible.
The first table deals with text types as the source of the assignment.
Target | Source | |||
---|---|---|---|---|
CHAR | MBCHAR | STRING, UNICODE | DBCHAR | |
CHAR | Y | Y | Y | |
MBCHAR | Y | Y | Y | |
STRING, UNICODE | Y | Y | Y | Y |
DBCHAR | Y | Y | ||
BIN | 1 | 1 | 1 | |
INT, BIGINT, SMALLINT | 1 | 1 | 1 | |
DECIMAL | 1 | 1 | 1 | |
NUM | 1 | 1 | 1 | |
NUMBER | 1 | 1 | 1 | |
FLOAT, SMALLFLOAT | 1 | 1 | 1 | |
MONEY | 1 | 1 | 1 | |
NUMC | 1 | 1 | 1 | |
PACF | 1 | 1 | 1 | |
DATE | 2 | 2 | 2 | |
INTERVAL (months) | 2 | 2 | 2 | |
INTERVAL (seconds) | 2 | 2 | 2 | |
TIME | 2 | 2 | 2 | |
TIMESTAMP | 2 | 2 | 2 | |
HEX | Y | Y | ||
BOOLEAN |
The second table deals with numeric types as the source of the assignment.
Target | Source | ||||||||
---|---|---|---|---|---|---|---|---|---|
BIN | INT, |
DECI- |
NUM | NUM- |
FLOAT, |
MONEY | NUMC | PACF | |
CHAR | Y | Y | Y | Y | Y | Y | 3 | Y | Y |
MBCHAR | Y | Y | Y | Y | Y | Y | 3 | Y | Y |
STRING | 4 | 4 | 4 | 4 | 4 | 4 | 3, 4 | 4 | 4 |
UNICODE | Y | Y | Y | Y | Y | Y | 3 | Y | Y |
DBCHAR | Y | Y | Y | Y | Y | Y | Y | Y | Y |
BIN | Y | Y | Y | Y | Y | Y | Y | Y | Y |
INT, BIGINT, SMALLINT | Y | Y | Y | Y | Y | Y | Y | Y | Y |
DECIMAL | Y | Y | Y | Y | Y | Y | Y | Y | Y |
NUM | Y | Y | Y | Y | Y | Y | Y | Y | Y |
NUMBER | Y | Y | Y | Y | Y | Y | Y | Y | Y |
FLOAT, |
Y | Y | Y | Y | Y | Y | Y | Y | Y |
MONEY | Y | Y | Y | Y | Y | Y | Y | Y | Y |
NUMC | Y | Y | Y | Y | Y | Y | Y | Y | Y |
PACF | Y | Y | Y | Y | Y | Y | Y | Y | Y |
DATE | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 |
INTERVAL (months) | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 |
INTERVAL (seconds) | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 |
TIME | |||||||||
TIMESTAMP | |||||||||
HEX | 7 | ||||||||
BOOLEAN | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 8 |
The third table deals with date/time, hex, and Boolean types as the source of the assignment.
Target | Source | ||||||
---|---|---|---|---|---|---|---|
DATE | INTERVAL (months) | INTERVAL (seconds) | TIME | TIME- |
HEX | BOOLEAN | |
CHAR | 9 | 9 | 9 | 9 | 9 | Y | |
MBCHAR | 9 | 9 | 9 | 9 | 9 | ||
STRING, UNICODE | 9 | 9 | 9 | 9 | 9 | Y | |
DBCHAR | |||||||
BIN | 10 | Y | Y | 11 | |||
INT, BIGINT, SMALLINT | 10 | Y | Y | 11 | |||
DECIMAL | 10 | 12 | 12 | 11 | |||
NUM | 10 | 12 | 12 | 11 | |||
NUMBER | 10 | 12 | 12 | 11 | |||
FLOAT, SMALLFLOAT | 10 | Y | 11 | ||||
MONEY | 10 | 12 | 12 | 11 | |||
NUMC | 10 | 12 | 12 | 11 | |||
PACF | 10 | 12 | 12 | 11 | |||
DATE | Y | Y | |||||
INTERVAL (months) | Y | ||||||
INTERVAL (seconds) | Y | ||||||
TIME | Y | Y | |||||
TIMESTAMP | Y | Y | Y | ||||
HEX | Y | ||||||
BOOLEAN | Y |
Function main()
testBin BIN(9,3) = 38796.999;
testDate DATE;
StrLib.defaultDateFormat = "MM/dd/yyyy";
testDate = testBin;
writeStdout (testDate);
end
myInterval INTERVAL("yyMM") = 1208;
Here
EGL interprets the number 1208 as twelve years and eight months.
The following rules apply:myInterval INTERVAL("yyMM") = 8216;
EGL
assigns the interval 83 years and 4 months. There are only 12 months
in a year, so the "16" is treated as four months and another year
is added to the original 82.myInterval INTERVAL("yyMM") = 9925;
EGL
assigns the interval 1 year and 1 month. 25 months = 2 years and
1 month, so months = 01. The years value is 99, plus 2 carried from
the months = 101, which is truncated to 01.A value of any of the numeric types (including NUMC and PACF) can be assigned to a variable of any numeric type and size, and EGL does the conversions necessary to retain the value in the target format.
Non-significant zeros are added or truncated as needed. (Initial zeros in the integer part of a value are non-significant, as are trailing digits in the fraction part of a value.)
If you use V6 exception mode and set vgVar.handleOverflow accordingly, you can use the sysVar.overflowIndicator system variable to test whether an assignment or arithmetic calculation resulted in an overflow. For more information, see handleOverflow. If you do not use V6 exception mode, or do not handle overflows, an overflow will cause EGL to throw a RuntimeException.
In an assignment statement, extra decimal digits are truncated when the truncateExtraDecimals build descriptor option is YES (the default; for more information see the EGL Generation Guide), unless the source is a FLOAT or SMALLFLOAT and the target has a fixed number of decimals. Extra decimal digits are rounded when truncateExtraDecimlas is set to NO or the source is a FLOAT or SMALLFLOAT and the target has a fixed number of decimals.
In version 6 and previous versions of EGL, the truncateExtraDecimals option did not exist, so extra decimals were always truncated unless the source was a FLOAT or SMALLFLOAT and the target had a fixed number of decimals. An additional rule prevented truncation if the source of the assignment was one of the MathLib functions. If you need to maintain this V6 style rounding, use the MathLib.assign() function (see assign()).
EGL truncates values on the right if the target of a character type has insufficient space to store the source value. No error is signaled.
In this situation, EGL truncates characters as needed to ensure that the target variable contains a valid string of type MBCHAR, then adds (if necessary) terminating single-byte blanks.
Nullable variables are reference compatible only with other nullable variables that have the exact same base type. The assignment follows the rules described in Reference compatibility in EGL.
sourceTimeStamp timestamp ("MMdd");
targetTimeStamp timestamp ("yyyyMMdd");
sourceTimeStamp = "1201";
// if this code runs in 2004, the next statement
// assigns 20041201 to targetTimeStamp
targetTimeStamp = sourceTimeStamp;
sourceTimeStamp02 timestamp ("ssff");
targetTimeStamp02 timestamp ("mmssff");
sourceTimeStamp02 = "3201";
// the next assignment includes the minute
// that is current when the assignment statement runs
targetTimeStamp02 = sourceTimeStamp02;
sourceTimeStamp timestamp ("yyyyMM");
targetTimeStamp timestamp ("yyyyMMdd");
sourceTimeStamp = "200412";
// regardless of the day, the next statement
// assigns 20041201 to targetTimeStamp
targetTimeStamp = sourceTimeStamp;
sourceTimeStamp02 timestamp ("hh");
targetTimeStamp02 timestamp ("hhmm");
sourceTimeStamp02 = "11";
// regardless of the minute, the next statement
// assigns 1100 to targetTimeStamp02
targetTimeStamp02 = sourceTimeStamp02;
You can assign a substructure field to a non-substructure field or the reverse, and you can assign values between two substructure fields. Assume, for example, that variables named myNum and myRecord are based on the following parts:
DataItem Num12
NUM(12)
end
Record ExampleRecord type basicRecord
10 topMost CHAR(4);
20 next01 HEX(4);
20 next02 HEX(4);
end
The assignment of a value of type HEX to a variable of type NUM is not valid outside of the mathematical system variables; but an assignment of the form myNum = topMost is valid because topMost is of type CHAR. In general terms, the primitive types of the fields in the assignment statement guide the assignment, and the primitive types of subordinate fields are not taken into account.
By default, the primitive type of a substructure field is CHAR. If you assign data to or from a substructure field and do not specify a different primitive type at declaration time, the rules described earlier for fields of type CHAR are in effect during the assignment.
An assignment of one structured record variable to another is equivalent to assigning one substructure field of type CHAR to another. A mismatch in length adds single-byte blanks to the right of the received value or removes single-byte characters from the right of the received value. The assignment does not consider the primitive types of subordinate structure fields.
Finally, if you assign a structured SQL record variable to or from a structured record variable of a different type, you must ensure that the non-SQL record has space for the four-byte area that precedes each structure field (for more information, see Structured records).
Non-structured record variables are assignment compatible only with other non-structured record variables of exactly the same type.
Due to truncation of intermediate results, COBOL programs might have different results than Java™ or Rich UI programs for the same arithmetic statements. To ensure consistent results across environments, use only one binary operator per statement. Multiple addition and subtraction operators can be safely combined if the number of decimal places defined for the result item is greater than or equal to the number of decimal places in any of the operands.
quotient = dividend / divisor;
remainder = dividend - (quotient * divisor);
Platform | Issue |
---|---|
COBOL generation | EGL is unable to convert text to SMALLFLOAT or FLOAT, though it can convert in the other direction. |