Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Change long raw datatype to character column

Oracle Database Tips by Donald BurlesonDecember 14, 2015

Question:  I have a long raw column within a table and I need to change the column datatype from long raw to varchar2.  How can I alter a long raw datatype to a character datatype?

Answer:  There are many methods changing a long raw to character values, but here is a test script that demonstrates a method.  The code is tricky because a long raw datatype is hexadecimal.

The script below takes the long raw and moves it into a new column.  At the end, you can alter the table to rename the columns to match the original name, if desired and drop the long raw column from the table.

set feedback off
set long 32767

drop table testtab;

create table
testtab
(col_key varchar2(10),
col_raw long raw);

-- *****************************************
-- Insert a character into a long raw column
--******************************************
insert into testtab values ('row 1', utl_raw.cast_to_raw('hello world'));

insert into testtab values ('row 2', utl_raw.cast_to_raw('hello moon'));


-- *****************************************
-- Because long raw is in hex, you have to
-- loop through it a byte at a time
-- ****************************************
declare
a varchar(255);
b long raw;
c varchar(255);


cursor c1 is select col_key, col_raw from testtab;
begin
open c1;
loop
   fetch c1 into a, b;
   exit when c1%notfound;
   a:= UTL_RAW.CAST_TO_VARCHAR2(b);
   dbms_output.put_line(a);
end loop;
end;
/

-- ***********************************************
-- Here we add a new column for the rato to text conversion
-- ***********************************************
alter table testtab add col_char varchar2(2000);

-- ***********************************************
-- We must create the insert column statement here
-- ***********************************************

spool runme.sql

-- *****************************************
-- Here we loop through each row in the table
-- ****************************************
declare
a varchar(255);
b long raw;
c varchar(255);
z varchar(255);

cursor c1 is select col_key, col_raw from testtab;
begin
open c1;
loop
   fetch c1 into a, b;
   exit when c1%notfound;
   z:= UTL_RAW.CAST_TO_VARCHAR2(b);
   dbms_output.put_line('update testtab set col_char = '''||z||''' where col_key = '''||a||''';');
end loop;
end;
/

spool off;

@runme.sql

-- ******************************************
-- Now select the current table value
-- ******************************************

select * from testtab;

alter table testtab drop column col_raw;
-- ************************************
--  Remember to rebuild any indexes/constraints that
--  might exist on the above table column
-- ***************************************

alter table testtab rename col_char to col_raw;

-- ******************************************
-- As a final step you may want to reorganize
-- the table to clean-up fragmentation
-- ******************************************

create table testtab_new as select * from testtab;

truncate table testtab;

insert into testtab (select * from testtab_new)

 

 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster