Conversion of Number Data Types
A type-promotion algorithm is used to evaluate expressions that contain more than one number data type.
Number data types are ranked from highest to lowest, as follows:
-
decimal
-
double
-
real
-
int
If the application encounters expressions that have more than one number data type among the operands, it converts all of the operands to the data type of the operand with the highest ranking type.
For example, if A is an int and B is a double, the expression A+B is evaluated by first converting A to double and then adding the two double values. The result is type double.
If in addition to A and B, you multiply the result by a decimal number C, then (A+B)*C is evaluated by first converting (A+B) to decimal, and then performing the indicated operations on the two decimal values. The result is type decimal.
For addition, subtraction, and multiplication, the operation result will be equal to the higher of the two operands. For example:
int + double = double
The following algorithm is used for division:
|
Numerator Data Type |
Denominator Data Type |
|||
|---|---|---|---|---|
|
int |
real |
double |
decimal(p,s1) |
|
|
int |
double |
double |
double |
decimal(p,s1) |
|
real |
double |
double |
double |
decimal(p,s1) |
|
double |
double |
double |
double |
decimal(p,s1) |
|
decimal(p,s2) |
decimal(p,s2) |
decimal(p,s2) |
decimal(p,s2) |
decimal(p,max(10,s1,s2)) |
Conversion of Decimals with Different Scale or Precision
If decimals of two different scales are included in a single expression, the application uses the higher of the two scales. For example:
decimal(5,4) * decimal(7,2) = decimal(7,2)
Expect a loss of precision when operating on two decimals of different scale values. For example, when adding a decimal(28, 26) to a decimal(28,1), the resulting decimal value has the lower of the two scale values:
400000.5 + 40.00005 = 400040.5
The least scale for division involving a decimal is 10.
Conversion Between Strings and Numbers
When the application encounters a string where a number would normally be expected (for example, in mathematical operations or functions that expect numeric arguments), it attempts to convert the string to a number.
For multiplication and division operations, operands are converted to numbers. Other promotion algorithms are shown in the following table.
|
Provided Data Type |
Data Type Required to Evaluate the Expression |
||
|---|---|---|---|
|
Number |
Date/time |
String |
|
|
Number |
OK (promoted) |
Number to interval |
String to number |
|
Date/time |
Number to interval |
Illegal |
String to interval |
|
String |
String to number |
String to interval |
String to real |
|
Provided Data Type |
Data Type Required to Evaluate the Expression |
|||
|---|---|---|---|---|
|
Number |
Date/time |
String |
Interval |
|
|
Number |
OK (promoted) |
Illegal |
String to number |
Interval to number |
|
Date/time |
Number to interval |
OK |
String to interval |
OK |
|
String |
String to number |
String to datetime |
String to real |
String to number |
|
Interval |
Number to interval |
Illegal |
String to interval |
OK |
|
Provided Data Type |
Data Type Required to Evaluate the Expression |
|||
|---|---|---|---|---|
|
Number |
Date/time |
String |
Interval |
|
|
Number |
OK (promoted) |
Illegal |
String to number |
OK |
|
Date/time |
Illegal |
OK |
Illegal |
Illegal |
|
String |
Illegal |
String to datetime |
OK |
String to interval |
|
Interval |
OK |
Illegal |
String to interval |
OK |