Oracle7 Server Application Developer's Guide

Contents Index Home Previous Next

Data Conversion

In some cases, Oracle allows data of one datatype where it expects data of a different datatype. Generally, an expression cannot contain values with different datatypes. However, Oracle can use the following functions to automatically convert data to the expected datatype:

Implicit datatype conversions work according to the rules explained in the following two sections.

Note: In this discussion, CHAR is used to refer to the class of all character string datatypes.

If using Trusted Oracle, see [*] for additional information involving data conversions and the MLSLABEL and RAW MLSLABEL datatypes.

Rule 1: Assignments

For assignments, Oracle can perform the following conversions automatically

The assignment succeeds if Oracle can convert the datatype of the value used in the assignment to that of the assignment's target. The four types of assignments are explained below.

For the examples in the following list, assume a package with a public variable declared as

var1   CHAR(5);

and a table created with the following statement:

CREATE TABLE table1 (col1 NUMBER);

		var1 := 0

		INSERT INTO table1 VALUES ('19');

		UPDATE table1 SET col1 = '30';

		SELECT col1 INTO var1 FROM table1 WHERE col1 = 30;

Rule 2: Expression Evaluation

For expression evaluation, Oracle can automatically perform the same conversions as for assignments. An expression is converted to a type based on its context. For example, operands to arithmetic operators are converted to NUMBER and operands to string functions are converted to VARCHAR2.

Some common types of expressions follow:

		comm + '500'

		bonus > sal / '10'

		MOD (counter, '2')

		WHERE hiredate = TO_DATE('1993-01-01','yyyy-mm-dd')

		WHERE rowid = '00000DC5.000C.0001'

In general, Oracle uses the rule for expression evaluation when a datatype conversion is needed in places not covered by the rule for assignment conversions.

In assignments of the form

variable := expression

Oracle first evaluates expression using the conversions covered by Rule 2; expression can be as simple or complex as desired. If it succeeds, the evaluation of expression results in a single value and datatype. Then, Oracle tries to assign this value to the assignment's target using Rule 1.

CHAR to NUMBER conversions only succeed if the character string represents a valid number. CHAR to DATE conversions only succeed if the character string satisfies the session default format, NLS_DATE_FORMAT.


Contents Index Home Previous Next