Oracle7 Server SQL Reference

Contents Index Home Previous Next

Datatypes

Each literal or column value manipulated by Oracle7 has a datatype. A value's datatype associates a fixed set of properties with the value. These properties cause Oracle7 to treat values of one datatype differently from values of another. For example, you can add values of NUMBER datatype, but not values of RAW datatype.

When you create a table or cluster, you must specify an internal datatype for each of its columns. When you create a procedure or stored function, you must specify an internal datatype for each of its arguments. These datatypes define the domain of values that each column can contain or each argument can have. For example, DATE columns cannot accept the value February 29 (except for a leap year) or the values 2 or 'SHOE'. Each value subsequently placed in a column assumes the column's datatype. For example, if you insert '01-JAN-92' into a DATE column, Oracle7 treats the '01-JAN-92' character string as a DATE value after verifying that it translates to a valid date.

Table 2 - 1 summarizes Oracle7 internal datatypes. The rest of this section describes these datatypes in detail.

Note: The Oracle precompilers recognize other datatypes in embedded SQL programs. These datatypes are called external datatypes and are associated with host variables. Do not confuse the internal datatypes with external datatypes. For information on external datatypes, including how Oracle7 converts between internal and external datatypes, see Programmer's Guide to the Oracle Precompilers.

Code Internal Datatype Description
1 VARCHAR2(size) Variable length character string having maximum length size bytes. Maximum size is 2000, and minimum is 1. You must specify size for a VARCHAR2
2 NUMBER(p,s) Number having precision p and scale s. The precision p can range from 1 to 38. The scale s can range from -84 to 127.
8 LONG Character data of variable length up to 2 gigabytes, or 231 -1 bytes.
12 DATE Valid date range from January 1, 4712 BC to December 31, 4712 AD.
23 RAW(size) Raw binary data of length size bytes. Maximum size is 255 bytes. You must specify size for a RAW value.
24 LONG RAW Raw binary data of variable length up to 2 gigabytes.
69 ROWID (see note below) Hexadecimal string representing the unique address of a row in its table. This datatype is primarily for values returned by the ROWID pseudocolumn.
96 CHAR(size) Fixed length character data of length size bytes. Maximum size is 255. Default and minimum size is 1 byte.
106 MLSLABEL Binary format of an operating system label. This datatype is used with Trusted Oracle7.
Table 2 - 1. Internal Datatype Summary

The codes listed for the datatypes are used internally by Oracle7. The datatype code of a column is returned when you use the DUMP function.

Note: The DESCRIBE embedded SQL command and the ODESCR call of the Oracle Call Interfaces (OCIs) returns a code of 11 for the ROWID datatype.

Character Datatypes

Character datatypes are used to manipulate words and free-form text. These datatypes are used to store character (alphanumeric) data in the database character set. They are less restrictive than other datatypes and consequently have fewer properties. For example, character columns can store all alphanumeric values, but NUMBER columns can only store numeric values.

Character data is stored in strings with byte values corresponding to the character set, such as 7-bit ASCII or EBCDIC Code Page 500, specified when the database was created. Oracle7 supports both single-byte and multi-byte character sets.

These datatypes are used for character data:

The character datatypes in Oracle7 are different from those in Oracle Version 6. For a summary of the differences and compatibility issues, see Appendix C "Operating System -Specific Dependendies" of this manual.

CHAR Datatype

The CHAR datatype specifies a fixed length character string. When you create a table with a CHAR column, you can supply the column length in bytes. Oracle7 subsequently ensures that all values stored in that column have this length. If you insert a value that is shorter than the column length, Oracle7 blank-pads the value to column length. If you try to insert a value that is too long for the column, Oracle7 returns an error.

The default for a CHAR column is 1 character and the maximum allowed is 255 characters. A zero-length string can be inserted into a CHAR column, but the column is blank-padded to 1 character when used in comparisons. For information on comparison semantics, see the section "Datatype Comparison Rules" [*].

VARCHAR2 Datatype

The VARCHAR2 datatype specifies a variable length character string. When you create a VARCHAR2 column, you can supply the maximum number of bytes of data that it can hold. Oracle7 subsequently stores each value in the column exactly as you specify it, provided it does not exceed the column's maximum length. This maximum must be at least 1 byte, although the actual length of the string stored is permitted to be zero. If you try to insert a value that exceeds the specified length, Oracle7 returns an error.

You must specify a maximum length for a VARCHAR2 column. The maximum length of VARCHAR2 data is 2000 bytes. Oracle7 compares VARCHAR2 values using non-padded comparison semantics. For information on comparison semantics, see the section "Datatype Comparison Rules" [*].

VARCHAR Datatype

The VARCHAR datatype is currently synonymous with the VARCHAR2 datatype. It is recommended that you use VARCHAR2 rather than VARCHAR. In a future version of Oracle7, VARCHAR might be a separate datatype used for variable length character strings compared with different comparison semantics.

NUMBER Datatype

The NUMBER datatype is used to store zero, positive and negative fixed and floating point numbers with magnitudes between 1.0 x 10-130 and 9.9...9 x 10125 (38 9s followed by 88 0s) with 38 digits of precision. If you specify an arithmetic expression whose value has a magnitude greater than or equal to 1.0 x 10126, Oracle7 returns an error.

You can specify a fixed point number using the following form:

NUMBER(p,s)

where:

p

is the precision, or the total number of digits. Oracle7 guarantees the portability of numbers with precision ranging from 1 to 38.

s

is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127.

You specify an integer using the following form:

NUMBER(p)

is a fixed point number with precision p and scale 0. (Equivalent to NUMBER(p,0).)

You specify a floating point number using the following form:

NUMBER

is a floating point number with precision 38. Note that a scale value is not applicable for floating point numbers.

Scale and Precision

Specify the scale and precision of a fixed point number column for extra integrity checking on input. Specifying scale and precision does not force all values to a fixed length. If a value exceeds the precision, Oracle7 returns an error. If a value exceeds the scale, Oracle7 rounds it.

The following examples show how Oracle7 stores data using different precisions and scales.

Actual Data

Specified As

Stored As

7456123.89

NUMBER

7456123.89

7456123.89

NUMBER(9)

7456124

7456123.89

NUMBER(9,2)

7456123.89

7456123.89

NUMBER(9,1)

7456123.9

7456123.8

NUMBER(6)

exceeds precision

7456123.8

NUMBER(15,1)

7456123.8

7456123.89

NUMBER(7,-2)

7456100

7456123.89

NUMBER(-7,2)

exceeds precision

Negative Scale

If the scale is negative, the actual data is rounded to the specified number of places to the left of the decimal point. For example, a specification of (10,-2) means to round to hundreds.

Scale Greater than Precision

You can specify a scale that is greater than precision, although it is uncommon. In this case, the precision specifies the maximum number of digits to the right of the decimal point. As with all number datatypes, if the value exceeds the precision, Oracle7 returns an error message. If the value exceeds the scale, Oracle7 rounds the value. For example, a column defined as NUMBER(4,5) requires a zero for the first digit after the decimal point and rounds all values past the fifth digit after the decimal point. The following examples show the effects of a scale greater than precision:

Actual Data

Specified As

Stored As

.01234

NUMBER(4,5)

.01234

.00012

NUMBER(4,5)

.00012

.000127

NUMBER(4,5)

.00013

.0000012

NUMBER(2,7)

.0000012

.00000123

NUMBER(2,7)

.0000012

Floating Point Numbers

Oracle7 also allows you to specify floating point numbers. A floating point value either can have a decimal point anywhere from the first to the last digit or can omit the decimal point altogether. A scale value is not applicable to floating point numbers because there is no restriction on the number of digits that can appear after the decimal point.

You can specify floating point numbers with the appropriate forms of the NUMBER datatype discussed in the section "NUMBER Datatype" [*]. Oracle7 also supports the ANSI datatype FLOAT. You can specify this datatype using one of these syntactic forms:

FLOAT

specifies a floating point number with decimal precision 38, or a binary precision of 126.

FLOAT(b)

specifies a floating point number with binary precision b. The precision b can range from 1 to 126.

To convert from binary to decimal precision, multiply b by 0.30103. To convert from decimal to binary precision, multiply the decimal precision by 3.32193. The maximum of 126 digits of binary precision is roughly equivalent to 38 digits of decimal precision.

LONG Datatype

LONG columns store variable length character strings containing up to 2 gigabytes, or 231-1 bytes. LONG columns have many of the characteristics of VARCHAR2 columns. You can use LONG columns to store long text strings. Oracle7 uses LONG columns in the data dictionary to store the text of view definitions. The length of LONG values may also be limited by the memory available on your computer.

You can reference LONG columns in SQL statements in these places:

The use of LONG values are subject to some restrictions:

Also, LONG columns cannot appear in certain SQL statements:

Also, LONG columns cannot appear in certain parts of SQL statements:

Triggers can use the LONG datatype in the following manner:

You can use the Oracle Call Interfaces to retrieve a portion of a LONG value from the database. See Programmer's Guide to the Oracle Call Interface.

DATE Datatype

The DATE datatype is used to store date and time information. Although date and time information can be represented in both CHAR and NUMBER datatypes, the DATE datatype has special associated properties.

For each DATE value the following information is stored:

To specify a date value, you must convert a character or numeric value to a data value with the TO_DATE function. Oracle7 automatically converts character values that are in the default date format into date values when they are used in date expressions. The default date format is specified by the initialization parameter NLS_DATE_FORMAT and is a string such as 'DD-MON-YY'. This example date format includes a two-digit number for the day of the month, an abbreviation of the month name, and the last two digits of the year.

If you specify a date value without a time component, the default time is 12:00:00a.m. (midnight). If you specify a date value without a date, the default date is the first day of the current month.

The date function SYSDATE returns the current date and time. For information on the SYSDATE and TO_DATE functions and the default date format, see Chapter 3 "Operators, Functions, Expressions, Conditions" of this manual.

Date Arithmetic

You can add and subtract number constants as well as other dates from dates. Oracle7 interprets number constants in arithmetic date expressions as numbers of days. For example, SYSDATE + 1 is tomorrow. SYSDATE - 7 is one week ago. SYSDATE + (10/1440) is ten minutes from now. Subtracting the HIREDATE column of the EMP table from SYSDATE returns the number of days since each employee was hired. You cannot multiply or divide DATE values.

Oracle7 provides functions for many of the common date operations. For example, the ADD_MONTHS function allows you to add or subtract months from a date. The MONTHS_BETWEEN function returns the number of months between two dates. The fractional portion of the result represents that portion of a 31-day month. For more information on date functions, see the section "Date Functions" [*].

Because each date contains a time component, most results of date operations include a fraction. This fraction means a portion of one day. For example, 1.5 days is 36 hours.

Using Julian Dates

A Julian date is the number of days since Jan 1, 4712 BC. Julian dates allow continuous dating from a common reference. You can use the date format model "J" with date functions TO_DATE and TO_CHAR to convert between Oracle7 DATE values and their Julian equivalents.

Example

This statement returns the Julian equivalent of January 1, 1992:

SELECT TO_CHAR(TO_DATE('01-01-1992', 'MM-DD-YYYY'),'J')
    FROM DUAL
TO_CHAR(TO_DATE('01-01-1992','MM-DD-YYYY),'J')
----------------------------------------------
2448623

RAW and LONG RAW Datatypes

The RAW and LONG RAW datatypes are used for data that is not to be interpreted (not converted when moving data between different systems) by Oracle. These datatypes are intended for binary data or byte strings. For example, LONG RAW can be used to store graphics, sound, documents, or arrays of binary data; the interpretation is dependent on the use.

RAW is a variable-length datatype like the VARCHAR2 character datatype, except that SQL*Net (which connects user sessions to the instance) and the Import and Export utilities do not perform character conversion when transmitting RAW or LONG RAW data. In contrast, SQL*Net and Import/Export automatically convert CHAR, VARCHAR2, and LONG data between the database character set to the user session character set (set by the NLS_LANGUAGE parameter of the ALTER SESSION command), if the two character sets are different.

When Oracle automatically converts RAW or LONG RAW data to and from CHAR data, the binary data is represented in hexadecimal form with one hexadecimal character representing every four bits of RAW data. For example, one byte of RAW data with bits 11001011 is displayed and entered as 'CB'.

LONG RAW data cannot be indexed, but RAW data can be indexed.

ROWID Datatype

Each row in the database has an address. You can examine a row's address by querying the pseudocolumn ROWID. Values of this pseudocolumn are hexadecimal strings representing the address of each row. These string have the datatype ROWID. For more information on the ROWID pseudocolumn, see the section "Pseudocolumns" [*]. You can also create tables and clusters that contain actual columns having the ROWID datatype. Oracle7 does not guarantee that the values of such columns are valid ROWIDs.

Character values representing ROWIDs:

block.row.file

where:

block

is a hexadecimal string identifying the data block of the data file containing the row. The length of this string may vary depending on your operating system.

row

is a four-digit hexadecimal string identifying the row in the data block. The first row in the block has the number 0.

file

is a hexadecimal string identifying the database file containing the row. The first data file has the number 1. The length of this string may vary depending on your operating system.

Example

Consider this ROWID value:

0000000F.0000.0002

The row corresponding to this ROWID is the first row (0000) in the fifteenth data block (0000000F) of the second data file (0002).

MLSLABEL Datatype

The MLSLABEL datatype is used to store the binary format a label used on a secure operating system. Labels are used by Trusted Oracle7 to mediate access to information. You can also define columns with this datatype if you are using the standard Oracle7 Server. For more information on Trusted Oracle7, including this datatype and labels, see Trusted Oracle7 Server Administrator's Guide.

ANSI, DB2, and SQL/DS Datatypes

SQL commands that create tables and clusters can also both ANSI datatypes and datatypes from IBM's products SQL/DS and DB2. Oracle7 creates columns with Oracle7 datatypes based on the conversions defined in Table 2 - 2 and Table 2 - 3.

ANSI SQL Datatype

Oracle7 Datatype

CHARACTER(n) CHAR(n)

CHAR(n)

CHARACTER VARYING(n) CHAR VARYING(n)

VARCHAR(n)

NUMERIC(p,s) DECIMAL(p,s)

NUMBER(p,s)

INTEGER INT SMALLINT

NUMBER(38)

FLOAT(b) 2 DOUBLE PRECISION 3 REAL 4

NUMBER

Table 2 - 2. ANSI Datatypes Converted to Oracle7 Datatypes

SQL/DS or DB2 Datatype

Oracle7 Datatype

CHARACTER(n)

CHAR(n)

VARCHAR(n)

VARCHAR(n)

LONG VARCHAR(n)

LONG

DECIMAL(p,s) 1

NUMBER(p,s)

INTEGER SMALLINT

NUMBER(38)

FLOAT(b) 2

NUMBER

Table 2 - 3. SQL/DS and DB2 Datatypes Converted to Oracle7 Datatypes

1 The NUMERIC, DECIMAL, and DEC datatypes can specify only fixed point numbers. For these datatypes, s defaults to 0. 2 The FLOAT datatype is a floating point number with a binary precision b. This default precision for this datatype is 126 binary, or 38 decimal. 3 The DOUBLE PRECISION datatype is a floating point number with binary precision 126. 4 The REAL datatype is a floating point number with a binary precision of 63, or 18 decimal.

Do not define columns with these SQL/DS and DB2 datatypes because they have no corresponding Oracle7 datatype:

Note that data of type TIME and TIMESTAMP can also be expressed as Oracle7 DATE data.

Datatype Comparison Rules

This section describes how Oracle7 compares values of each datatype.

Number Values

A larger value is considered greater than a smaller one. All negative numbers are less than zero and all positive numbers. Thus, -1 is less than 100; -100 is less than -1.

Date Values

A later date is considered greater than an earlier one. For example, the date equivalent of '29-MAR-1991' is less than that of '05-JAN-1992' and '05-JAN-1992 1:35pm' is greater than '05-JAN-1992 10:09am'.

Character String Values

Character values are compared using one of these comparison rules:

The following sections explain these comparison semantics. The results of comparing two character values using different comparison semantics may be different. Table 2 - 4 shows the results of comparing five pairs of character values using each comparison semantic. The last comparison in the table illustrates the differences between the blank-padded and non-padded comparison semantics.

The results of blank-padded and non-padded comparisons is shown in Table 2 - 4. Usually, the results of blank-padded and non-padded comparisons are the same. However, note the exception highlighted in bold in Table 2 - 4 where blanks are considered less than any character, which is true in most character sets.

Blank-Padded

Non-Padded

'ab' > 'aa'

'ab' > 'aa'

'ab' > 'a?'

'ab' > 'a?'

'ab' > 'a'

'ab' > 'a'

'ab' = 'ab'

'ab' = 'ab'

'a?' = 'a'

'a?' > 'a'

Table 2 - 4. Results of Comparisons with Blank-Padded and Non-Padded Comparison Semantics

Blank-Padded Comparison Semantics If the two values have different lengths, Oracle7 first adds blanks to the end of the shorter one so their lengths are equal. Oracle7 then compares the values character by character up to the first character that differs. The value with the greater character in the first differing position is considered greater. If two values have no differing characters, then they are considered equal. This rule means that two values are equal if they differ only in the number of trailing blanks. Oracle7 uses blank-padded comparison semantics only when both values in the comparison are either expressions of datatype CHAR, text literals, or values returned by the USER function.

Non-Padded Comparison Semantics Oracle7 compares two values character by character up to the first character that differs. The value with the greater character in that position is considered greater. If two values of different length are identical up to the end of the shorter one, the longer value is considered greater. If two values of equal length have no differing characters, then the values are considered equal. Oracle7 uses non-padded comparison semantics whenever one or both values in the comparison have the datatype VARCHAR2.

Single Characters

Oracle7 compares single characters according to their numeric values in the database character set. One character is greater than another if it has a greater numeric value than the other in the character set. In Table 2 - 4, blanks are considered less than any character, which is true in most character sets.

These are some common character sets:

Portions of the ASCII and EBCDIC character sets appear in Table 2 - 5 and Table 2 - 6. Note that uppercase and lowercase letters are not equivalent. Also, note that the numeric values for the characters of a character set may not match the linguistic sequence for a particular language.

ASCII Character Set

Table 2 - 5 lists the 7-bit ASCII character set.

Decimal value

Symbol

Decimal value

Symbol

32

blank

59

;

33

!

60

<

34

"

61

=

35

#

62

>

36

$

63

?

37

%

64

@

38

&

65-90

A-Z

39

'

91

[

40

(

92

\

41

)

93

]

42

*

94

^^

43

+

95

_

44

,

96

`

45

-

97-122

a-z

46

.

123

{

47

/

124

|

48-57

0-9

125

}

58

:

126

~

Table 2 - 5. ASCII Character Set

EBCDIC Character Set

Table 2 - 6 lists a common portion of the EBCDIC character set.

Decimal value

Symbol

Decimal value

Symbol

64

blank

108

%

74

¢

109

_

75

.

110

>

76

<

111

?

77

(

122

:

78

+

123

#

79

|

124

@

80

&

125

'

90

!

126

=

91

$

127

"

92

*

129-137

a-i

93

)

145-153

j-r

94

;

162-169

s-z

95

¬

193-201

A-I

96

-

209-217

J-R

97

/

226-233

S-Z

Table 2 - 6. EBCDIC Character Set

Data Conversion

Generally an expression cannot contain values of different datatypes. For example, an expression cannot multiply 5 by 10 and then add 'JAMES'. However, Oracle7 supports both implicit and explicit conversion of values from one datatype to another.

Implicit Data Conversion

Oracle7 automatically converts a value from one datatype to another when such a conversion makes sense. Oracle7 performs datatype conversions in these cases:

Example I

The text literal '10' has datatype CHAR. Oracle7 implicitly converts it to the NUMBER datatype if it appears in a numeric expression as in the following statement:

SELECT sal + '10'
    FROM emp

Example II

When a condition compares a character value and a NUMBER value, Oracle7 implicitly converts the character value to a NUMBER value, rather than converting the NUMBER value to a character value. in the following statement, Oracle7 implicitly converts '7936' to 7936:

SELECT ename
    FROM emp 
	WHERE empno = '7936'

If the character value is too short to fit the entire number value, the number value is rounded.

Example III

In the following statement, Oracle7 implicitly converts '12-MAR-1993' to a DATE value using the default date format 'DD-MON-YYYY':

SELECT ename
    FROM emp 
	WHERE hiredate = '12-MAR-1993'

Example IV

In the following statement, Oracle7 implicitly converts the text literal '00002514.0001.0001' to a ROWID value:

SELECT ename
    FROM emp 
	WHERE ROWID = '00002514.0001.0001'

Explicit Data Conversion

You can also explicitly specify datatype conversions using SQL conversion functions. Table 2 - 7 shows SQL functions that explicitly convert a value from one datatype to another.

TO FROM

CHAR

NUMBER

DATE

RAW

ROWID

CHAR

unnecessary

TO_NUMBER

TO_DATE

HEXTORAW

CHARTOROWID

NUMBER

TO_CHAR

unnecessary

TO_DATE (number,'J')

DATE

TO_CHAR

TO_CHAR (date,'J')

unnecessary

RAW

RAWTOHEX

unnecessary

ROWID

ROWIDTOCHAR

unnecessary

Table 2 - 7. SQL Functions for Datatype Conversion

For information on these functions, see the section "Conversion Functions" [*].

Note: Note that Table 2 - 7 does not show conversions from LONG and LONG RAW values because it is impossible to specify LONG and LONG RAW values in cases in which Oracle7 can perform implicit datatype conversion. For example, LONG and LONG RAW values cannot appear in expressions with functions or operators. For information on the limitations on LONG and LONG RAW datatypes, see the section "LONG Datatype" [*].

Implicit vs. Explicit Data Conversion

It is recommended that you specify explicit conversions rather than rely on implicit or automatic conversions for these reasons:


Contents Index Home Previous Next