0

Can somebody please tell me how the utl_raw.convert works with utl_raw.cast_to_varchar2 in Oracle 11g by giving a sample code as i am not able to find an example use case online.

Thanks Gautam

Gautam S
  • 41
  • 1
  • 1
  • 7

1 Answers1

0

UTL_RAW.CONVERT is a function to convert a raw from one character set to another character set returning the result as a raw.

So, the functions admits 3 parameters:

  • The raw you want to convert
  • The characterset destination
  • The characterset source

UTL_RAW.CAST_TO_VARCHAR2 converts a raw string into a varchar2 data type.

Let's put it into test

My settings: Oracle 11g Database Character Set: 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
????????????

Here my representation of the second raw is not accurate, as I have to cast_to_raw in the first place. If you don't do that , you got ORA-O1465: invalid hex number.

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

Use as well the dump() function to investigate the byte values that make up the string. This way you can determine if the string is made up of the correct values.

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
Roberto Hernandez
  • 8,231
  • 3
  • 14
  • 43
  • Thanks for your valuable response . If an interface team sends a file with the following line with section symbol in it for e.g §test1§test2§ . How to read it with UTL_RAW, convert and cast and insert it into a table.Currently i am using utl_file.get_line and it is not reading the section symbol. My DB character set is UTF8 and the interface is sending the file with the charset iso-8859-1. The Oracle Database version is 11.2.0.4.0.1.Can you pl help me ? Thanks. – Gautam S Jul 12 '20 at 04:16
  • @GautamS , you need to edit the question and provide a sample of the text and the code you are using. – Roberto Hernandez Jul 12 '20 at 09:30