Database Reference
In-Depth Information
Second, the RAW data looks much larger than 16 bytes—in fact, in this example, you can see 32 characters. This is
due to the fact that every binary byte takes two hexadecimal characters to display (if the leading character is a zero, the
zero is not displayed). The stored RAW data is really 16 bytes in length, and you can see this using the Oracle SQL DUMP
function. Here, I am dumping the value of the binary string and using the optional parameter to specify the base that
should be used when displaying the value of each byte. I am using base 16, so we can compare the results of dump
with the previous string:
EODA@ORA12CR1> select dump(raw_data,16) from t;
DUMP(RAW_DATA,16)
-------------------------------------------------------------------------------
Typ=23 Len=16: ee,f1,8a,a3,b,56,3a,f0,e0,43,b7,d0,4f,a,4a,30
So, DUMP shows us this binary string is in fact 16 bytes long ( Len=16 ) and displays the binary data byte by byte.
As we can see, this dump display matches up with the implicit conversion performed when SQL*Plus fetched the RAW
data into a string. This implicit conversion goes the other direction as well:
EODA@ORA12CR1> insert into t values ( 'abcdef' );
1 row created.
That did not insert the string abcdef , but rather a 3-byte RAW with the bytes AB , CD , EF , or in decimal with the bytes
171, 205, 239 . If you attempt to use a string that does not consist of valid hex characters, you will receive an error
message:
EODA@ORA12CR1> insert into t values ( 'abcdefgh' );
insert into t values ( 'abcdefgh' )
*
ERROR at line 1:
ORA-01465: invalid hex number
The RAW type may be indexed and used in predicates—it is as functional as any other datatype. However, you
must take care to avoid unwanted implicit conversions, and you must be aware that they will occur.
I prefer and recommend using explicit conversions in all cases, which can be performed using the following
built-in functions:
HEXTORAW : To convert strings of hexadecimal characters to the RAW type
RAWTOHEX : To convert RAW strings to hexadecimal strings
The RAWTOHEX function is invoked implicitly by SQL*Plus when it fetches a RAW type into a string, and the
HEXTORAW function is invoked implicitly when inserting the string. It is a good practice to avoid implicit conversions
and to always be explicit when coding. So the previous examples could have been written as follows:
EODA@ORA12CR1> select rawtohex(raw_data) from t;
RAWTOHEX(RAW_DATA)
--------------------------------
EEF18AA30B563AF0E043B7D04F0A4A30
EODA@ORA12CR1> insert into t values ( hextoraw('abcdef') );
1 row created.
 
Search WWH ::




Custom Search