05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

504<br />

CHAPTER 12 ■ DATATYPES<br />

ops$tkyte@ORA10GR1> insert into t values ( 'abcdefgh' );<br />

insert into t values ( 'abcdefgh' )<br />

*<br />

ERROR at line 1:<br />

ORA-01465: invalid hex number<br />

The RAW type may be indexed <strong>and</strong> used in predicates—it is as functional as any other<br />

datatype. However, you must take care to avoid unwanted implicit conversions, <strong>and</strong> you must<br />

be aware that they will occur.<br />

I prefer <strong>and</strong> recommend using explicit conversions in all cases, which can be performed<br />

using the following built-in functions:<br />

• HEXTORAW: To convert strings of hexadecimal characters to the RAW type<br />

• RAWTOHEX: To convert RAW strings to hexadecimal strings<br />

The RAWTOHEX function is invoked implicitly by SQL*Plus when it fetches a RAW type into a<br />

string, <strong>and</strong> the HEXTORAW function is invoked implicitly when inserting the string. It is a good<br />

practice to avoid implicit conversions <strong>and</strong> to always be explicit when coding. So the previous<br />

examples could have been written as follows:<br />

ops$tkyte@ORA10GR1> select rawtohex(raw_data) from t;<br />

RAWTOHEX(RAW_DATA)<br />

--------------------------------<br />

FD1EB03D3718077BE030007F01002FF5<br />

ops$tkyte@ORA10GR1> insert into t values ( hextoraw('abcdef') );<br />

1 row created.<br />

Number Types<br />

<strong>Oracle</strong> 10g supports three native datatypes suitable for storing numbers. <strong>Oracle</strong>9i Release 2<br />

<strong>and</strong> earlier support exactly one native datatype suitable for storing numeric data. In this list,<br />

the NUMBER type is supported by all releases, <strong>and</strong> the subsequent two types are new datatypes<br />

supported only in <strong>Oracle</strong> 10g <strong>and</strong> later:<br />

• NUMBER: The <strong>Oracle</strong> NUMBER type is capable of storing numbers with an extremely large<br />

degree of precision—38 digits of precision, in fact. The underlying data format is similar<br />

to a “packed decimal” representation. The <strong>Oracle</strong> NUMBER type is a variable-length format<br />

from 0 to 22 bytes in length. It is appropriate for storing any number as small as<br />

10e -130 <strong>and</strong> numbers up to but not including 10e 126 . This is by far the most common<br />

NUMBER type in use today.<br />

• BINARY_FLOAT: This is an IEEE native single-precision floating-point number. On disk<br />

it will consume 5 bytes of storage: 4 fixed bytes for the floating-point number <strong>and</strong> 1<br />

length byte. It is capable of storing numbers in the range of ~ ± 10 38.53 with 6 digits of<br />

precision.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!