Question

I am trying to convert string to long in oracle 8. How do I do that? I have only version from blob to char.

    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;

Cannot find anywhere on google.

Was it helpful?

Solution

I'm not sure if you're just over-thinking this. A varchar2 is always going to be smaller than a long or clob could be, so you can just assign your value. It's going the other way that causes problems. You can just do this in SQL:

insert into t42 (long_val, clob_val)
values ('any string up to 4000 chars', 'any other string up to 4000 chars');

Or from PL/SQL:

declare
  char_val varchar2(4000);
begin
  char_val := 'another string';
  insert into t42 (long_val, clob_val)
  values (char_val, char_val);
end;
/

SQl Fiddle demo. I believe that all worked the same in 8i as it does now in 11g.

The PL/SQL varchar2 can be up to 32k and still be inserted like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top