Back to Contents


Data Conversions

Summary:

See also: Data Types.


Data Conversion

The runtime system performs data conversion implicitly without objection, as long as the data conversion is valid. For example, when you assign a number expression to a character variable, the runtime system converts the resulting number to a literal string.

Conversion rules apply to variable assignment, function parameters, and returned values.

Data conversions from or to character string values involve environment and locale settings like DBDATE, DBFORMAT.

When using the default exception handler, if a conversion error occurs, STATUS is zero and the target is set to zero for SMALLINT, INTEGER, SMALLFLOAT and FLOAT data types or NULL for all other data types.

The global STATUS variable is not set when a conversion error occurs unless you have enabled ANY ERROR detection with the WHENEVER instruction.

Conversion Table

The following table shows which pairs of data types are compatible.

Conversion table:

Columns represent source data types and lines represent receiving data types.

  c
h
a
r
v
a
r
c
h
a
r
s
t
r
i
n
g
b
i
g
i
n
t
i
n
t
e
g
e
r
s
m
a
l
l
i
n
t
t
i
n
y
i
n
t
b
o
o
l
e
a
n
f
l
o
a
t
s
m
a
l
l
f
l
o
a
t
d
e
c
i
m
a
l
m
o
n
e
y
d
a
t
e
d
a
t
e
l
i
n
e
i
n
t
e
r
v
a
l
t
e
x
t
b
y
t
e
char 1 1 1 1 1 1 1 1 1 1 1 1, 8 1, 9 1 1 1 15
varchar 1 1 1 1 1 1 1 1 1 1 1 1, 8 1, 9 1 1 1 15
string 1 1 1 1 1 1 1 1 1 1 1 1, 8 1, 9 1 1 1 15
bigint 2, 3 2, 3 2, 3           3, 4 3, 4 3, 4 3, 4 11 15 15 15 15
integer 2, 3 2, 3 2, 3 3         3, 4 3, 4 3, 4 3, 4 11 15 15 15 15
smallint 2, 3 2, 3 2, 3 3 3       3, 4 3, 4 3, 4 3, 4 3, 11 15 15 15 15
tinyint 2, 3 2, 3 2, 3 3 3 3     3, 4 3, 4 3, 4 3, 4 3, 11 15 15 15 15
boolean 2, 3 2, 3 2, 3 3 3 3 3   3, 4 3, 4 3, 4 3, 4 3, 11 15 15 15 15
float 2, 3, 5 2, 3, 5 2, 3, 5 10 10 10 10 10     3 3 11 15 15 15 15
smallfloat 2, 3, 5 2, 3, 5 2, 3, 5 5, 10 5, 10 10 10 10 5   3, 5 3, 5 5, 11 15 15 15 15
decimal 2, 3, 6 2, 3, 6 2, 3, 6 3 3 3 3 3 3, 6 3, 6 3, 6 3, 6 3, 11 15 15 15 15
money 2, 3, 6 2, 3, 6 2, 3, 6 3 3 3 3 3 3, 6 3, 6 3, 6 3, 6 3, 11 15 15 15 15
date 2 2 2 11 11 11 11 11 3,4,11 3,4,11 3,4,11 3,4,11   12, 14 15 15 15
datetime 2 2 2 15 15 15 15 15 15 15 15 15 13, 14 7, 14 15 15 15
interval 2 2 2 15 15 15 15 15 15 15 15 15 15 15 3, 7 15 15
text 1 1 1 15 15 15 15 15 15 15 15 15 15 15 15   15
byte 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15  

Legend:

  1. If the result of converting a value to a character string is longer than the receiving variable, the character string is truncated from the right.
  2. Character string values must depict a literal of the receiving data type.
  3. If the value exceeds the range of the receiving data type, an overflow error occurs.
  4. Any fractional part of the value is truncated.
  5. If the passed value contains more significant digits than the receiving data type supports, low-order digits are discarded.
  6. If the passed value contains more fractional digits than the receiving data type supports, low-order digits are discarded.
  7. Differences in qualifiers can cause truncation from the left or right.
  8. DBMONEY and DBFORMAT control the format of the converted string.
  9. DBFORMAT, DBDATE, or GL_DATE controls the format of the result.
  10. Rounding errors can produce an assigned value with fractional parts.
  11. An integer value corresponding to a count of days is assigned. 
  12. An implicit EXTEND( value, YEAR TO DAY ) is performed.
  13. The DATE becomes a DATETIME YEAR TO DAY literal before assignment.
  14. If the passed value has less precision than the receiving variable, any missing time unit values are obtained from the system clock.
  15. Unsupported conversion.