tags:

views:

1830

answers:

3

I have an Oracle table that contains a field of LONG RAW type that contains ASCII character data. How can I write a query or view that will convert this to a more easily consumed character string? These are always going to be single-byte characters, FWIW.

A: 

I found this quote:

In Oracle9i, you can even:

alter table old_table modify ( c clob );

to convert it.

See here: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1037232794454

Edit

The max length of a varchar2 column is 4000. Is that too short?

tuinstoel
An existing and pretty brittle application currently uses this table, so I'd prefer not to modify the existing table for fear of knocking down the house of cards.
Chris Farmer
A: 

I have found this works well on CLOB data types. I would believe the same would hold true for LOB types.

create or replace function lob2char(clob_col clob) return varchar2 IS
buffer varchar2(4000);
amt BINARY_INTEGER := 4000;
pos INTEGER := 1;
l clob;
bfils bfile;
l_var varchar2(4000):='';
begin
LOOP
if dbms_lob.getlength(clob_col)<=4000 THEN
dbms_lob.read (clob_col, amt, pos, buffer);
l_var := l_var||buffer;
pos:=pos+amt;
ELSE
l_var:= 'Cannot convert.  Exceeded varchar2 limit';
exit;
END IF;
END LOOP;
return l_var;
EXCEPTION
WHEN NO_DATA_FOUND THEN
return l_var;
END;



INSERT INTO NEWTABLE (NEWCOLUMN) SELECT RTRIM(lob2char(OLDCOLUMN)) FROM OLDTABLE;
Bob
What's the LOOP for?
WW
+1  A: 

Maybe

select ...., to_lob(long_raw) from old_table

(http://www.psoug.org/reference/convert_func.html)

or

UTL_RAW.CAST_TO_VARCHAR2(b)

(http://www.dbasupport.com/forums/showthread.php?t=5342).

tuinstoel