CAST
建表操作:
create table raw_test (id number, raw_date raw(10));
插入raw数据操作:
insert into raw_test values (1, hextoraw('ff'));
insert into raw_test values (utl_raw.cast_to_raw('051'));
删除表操作:
drop table raw_test;
当使用HEXTORAW时,会把字符串中数据当作16进制数。而使用UTL_RAW.CAST_TO_RAW时,直接把字符串中每个字符的ASCII码存放到RAW类型的字段中.
可以使用dump函数,查询存储情况:
select id,raw_date, dump(raw_date, 16) dump_raw from raw_test;
Oracle中RAW和Varchar2常用的两个转换函数
1. UTL_RAW.CAST_TO_RAW
该函数按照缺省字符集(一般为GB2312),将VARCHAR2字符串转换为RAW。
insert into cmpp_submit (dest_terminal_id,msg_content) values('13001081371',UTL_RAW.CAST_TO_RAW('您好!'));
2. UTL_RAW.CAST_TO_VARCHAR2
该函数按照缺省字符集合(一般为GB2312),将RAW转换为VARCHAR2。
select UTL_RAW.CAST_TO_VARCHAR2(msg_content) from cmpp_deliver;
其实RAW和VARCHAR是类似的,只是存储在RAW里的是二进制值,在任何时候不会做自动的字符集转换,这是RAW和VARCHAR的不同,RAW只是一种外部类型,其内部存储是VARRAW
VARCHAR的Oracle内部定义是:struct { ub2 len; char arr[n] }
VARRAW的ORACLE内部定义是: struct { ub2 len; unsigned char arr[n] }
hextoraw:
英文介绍:HEXTORAW converts char containing hexadecimal digits in the CHAR, VARCHAR2, NCHAR, or
NVARCHAR2 character set to a raw value.
This function does not support CLOB data directly. However, CLOBs can be passed in as arguments
through implicit data conversion。
例子:
select hextoraw('1a') from dual
输出:1A
这里输入的参数必须要符合16进制的格式,比如:1A,a1等,从前到后都是两个字符组成一个十六进制数,若输入参数为奇数个字符,则函数会在最前面添加0,比如:
select hextoraw('1ca') from dual;
输出:01CA。
UTL_RAW.CAST_TO_RAW
英文介绍:
UTL_RAW.CAST_TO_RAW converts a value of data type VARCHAR2 into a raw value with the same number of bytes. The input value is treated as if it were composed of single 8-bit bytes, not characters. Multibyte character boundaries are ignored. The data is not modified in any way, it is only changed to data type RAW.
例子:
select UTL_RAW.CAST_TO_RAW('ABC') from dual;
输出:414243
解释:输出的是输入字符串的十六进制的值