您能提供 utl_raw.convert + utl_raw.cast_to_varchar2 的示例吗?

问题描述 投票:0回答:1

有人可以通过提供示例代码来告诉我

utl_raw.convert
如何与 Oracle 11g 中的
utl_raw.cast_to_varchar2
配合使用吗,因为我无法在线找到示例用例。

谢谢 高塔姆

oracle plsql utf-8 ascii file-handling
1个回答
0
投票

UTL_RAW.CONVERT 是一个将原始数据从一种字符集转换为另一种字符集并以原始数据形式返回结果的函数。

因此,该函数接受 3 个参数:

  • 您要转换的原始数据
  • 字符集目标
  • 字符集来源

UTL_RAW.CAST_TO_VARCHAR2 将原始字符串转换为 varchar2 数据类型。

我们来测试一下吧

我的设置:Oracle 11g 数据库字符集:UTF-8

SQL> create table test_raw ( c1 raw(256) ) ;

Table created.

SQL>  insert into test_raw select UTL_RAW.CAST_TO_RAW ( '123009988poee' ) from dual ;

1 row created.

SQL> insert into test_raw select UTL_RAW.CAST_TO_RAW ( 'üäöüöä' ) from dual ;

1 row created.

SQL> commit ;

Commit complete.

SQL> select * from test_raw ;

C1
--------------------------------------------------------------------------------
313233303039393838706F6565
EFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD

SQL> select utl_raw.cast_to_varchar2 ( a.c1 ) from test_raw a ;

UTL_RAW.CAST_TO_VARCHAR2(A.C1)
--------------------------------------------------------------------------------
123009988poee
????????????

这里我对第二个原始数据的表示不准确,因为我必须首先将其转换为原始数据。如果你不这样做,你会得到 ORA-O1465:无效的十六进制数。

SQL> select utl_raw.cast_to_varchar2 ( utl_raw.convert ( utl_raw.cast_to_raw ( a.c1 ) , 'we8mswin1252' , 'al32utf8' ) ) as result
  2  from test_raw a ;

RESULT
--------------------------------------------------------------------------------
313233303039393838706F6565
EFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD

还可以使用 dump() 函数来研究组成字符串的字节值。这样您就可以确定字符串是否由正确的值组成。

SQL> select dump ( utl_raw.cast_to_varchar2 ( utl_raw.convert ( utl_raw.cast_to_raw ( a.c1 ) , 'we8mswin1252' , 'al32utf8' )  ) ) as dump_result
from test_raw a

RESULT
--------------------------------------------------------------------------------
Typ=1 Len=26: 51,49,51,50,51,51,51,48,51,48,51,57,51,57,51,56,51,56,55,48,54,70,
54,53,54,53

Typ=1 Len=72: 69,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,
66,68,69,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,66,68,69
,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,66,68,69,70,66,70,66,68


SQL> select dump ( c1 ) from test_raw ;

DUMP(C1)
--------------------------------------------------------------------------------
Typ=23 Len=13: 49,50,51,48,48,57,57,56,56,112,111,101,101
Typ=23 Len=36: 239,191,189,239,191,189,239,191,189,239,191,189,239,191,189,239,1
91,189,239,191,189,239,191,189,239,191,189,239,191,189,239,191,189,239,191,189
© www.soinside.com 2019 - 2024. All rights reserved.