Convert clob to blob

Anyone know an easy way to convert clob data to blob data?
We upgraded a client to 8.1.7.2 and now we can no longer store MS-Word templates to the RDBMS.
I deduce this is because MS-Word templates are binary files.
Earlier versions of 8.1.7 and 8.1.6 allowed us to do this. However, this is no longer the case.
Some of my clob rows are over 6 meg.
I have written some PL/SQL code to basically retrieve the clob data in cursor, loops through the clob in 32767 blocks,
performing dbms_lob.substr, converts that data to hex(major pain), & then write the blocks using dbmbs_lob.write.
I keep getting the proverbial "ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275".
Does anyone see flaws in my approach and might have an easier solution?
Thanks!

Here's my code:
REATE OR REPLACE
PROCEDURE p_load_clob_to_blob IS
* Name:           p_load_clob_to_blob
* Parameters: None
* Purpose: This procedure loads the narr_blob column in the narr table_text with clob data from the
* narr column.
* Notes: This procedure assumes that narr table has been altered with narr_blob column added.
* See the SQL below:
*               alter table narr_text add (narr_blob blob);
* Once this procedure executes, the table must altered to drop the narr_text clob column,
* recreate the narr_text as blob, & then repopulate narr_text. See the SQL below:
*                     alter table narr_text drop column narr;
*                    alter table narr_text add (narr blob);
*                    update narr_text set narr = narr_blob;
*                    commit;
* Called By: Sys Admin
* CHANGE LOG
* Changed By Date Change Description
* EAO          01/16/02 Created.
rec_read      INTEGER;
rec_update      INTEGER;
write_cnt INTEGER;
write_amount BINARY_INTEGER;
write_offset INTEGER;
write_loop INTEGER;
total_length      NUMBER;
total_written      NUMBER;
buffer VARCHAR2(32767);
bbuffer RAW(32767);
temp_narr_id NUMBER;
max_loop               INTEGER;
cx                    CLOB;
bx                    BLOB;
bx2                    BLOB;
cur_evt               varchar2(50); -- current event
v_err_descr          varchar2(256);
i INTEGER;
hex                    varchar(32767);
CURSOR c_load_narr_clob IS
     SELECT narr, narr_id
     FROM narr_text
     where narr_id = 6366;
BEGIN
insert_event_log('', 'p_load_clob_to_blob', '', '', '', 'p_load_clob_to_blob started', '');
rec_read := 0;
rec_update := 0;
---Fill file
insert_event_log('', 'p_load_clob_to_blob ','c_load_narr_clob ', '', 'S', 'c_narr_clob started', '');
FOR csr IN c_load_narr_clob LOOP      
     cur_evt := 'Select narr from narr table: ';
     rec_read := rec_read + 1;
     write_loop := 1;
     write_cnt := 0;
     write_offset := 1;           
     total_written := 0;     
     cx := csr.narr;
bx := empty_blob();          
     total_length := DBMS_LOB.GETLENGTH(cx);
     max_loop := (total_length / 32767) + 1;
     if (total_length <= 32767) THEN
          write_amount := total_length;
     ELSE
          write_amount := 32767;
     END IF;
dbms_output.put_line('Length=' || to_char(total_length) || ' Max loop=' || to_char(max_loop) || ' Write Amount=' || to_char(write_amount));
     temp_narr_id := csr.narr_id;
     while write_cnt < max_loop
     loop
--FOR write_loop in 1..max_loop LOOP
          delete temp_blob;
          delete temp_raw;
          commit;
          cur_evt := 'Dbms_lob.substr: ';
          dbms_output.put_line(cur_evt);
buffer := DBMS_LOB.SUBSTR(cx, write_amount, write_offset);
          dbms_output.put_line('Write Amount='|| to_char(write_amount) || ' Write Offset=' || to_char(write_offset) );
cur_evt := 'Hex to Raw Assigment: ';
          dbms_output.put_line(cur_evt);
          bbuffer := null;
          for i in 1..write_amount loop
               hex := numtohex(ascii(substrb(buffer,i,1)));
               bbuffer := bbuffer || hextoraw(hex);
          end loop;
cur_evt := 'Insert temp_raw: ';
          dbms_output.put_line(cur_evt);
          insert into temp_raw(rx)
               values (bbuffer);
          commit;
cur_evt := 'Insert temp_blob: ';
          dbms_output.put_line(cur_evt);
          execute immediate 'insert into temp_blob(bx)
               select TO_LOB(rx) from temp_raw';
          commit;
          cur_evt := 'Select bx2: ';
          dbms_output.put_line(cur_evt);
          bx2 := empty_blob();
          select bx into bx2 from temp_blob;
          cur_evt := 'Dbms_lob.append: ';
          dbms_output.put_line(cur_evt);
          dbms_lob.append(bx, bx2);
     dbms_output.put_line('Write Amount='|| to_char(write_amount) || ' Write Offset=' || to_char(write_offset) );
          write_offset := write_offset + write_amount;
          total_written := total_written + write_amount;
          write_cnt := write_cnt + 1;
          if (write_cnt = max_loop) then
          write_amount := total_length - total_written;
          end if;
END LOOP;
dbms_output.put_line('Total_written = ' || to_char(total_written) );
     cur_evt := 'Upd narr_blob in narr table: ';
     update narr_text
     set narr_blob = bx
     where narr_id = temp_narr_id;
     rec_update := rec_update + 1;
     commit;
END LOOP;
insert_event_log('', 'p_load_clob_to_blob ','c_load_narr_clob ', '', 'C', 'c_narr_clob completed', '');
dbms_output.put_line('Records read=' || to_char(rec_read) || ' Records updated=' || to_char(rec_update) );
insert_event_log('', 'p_load_clob_to_blob', '', '', '', 'p_load_clob_to_blob ended', '');
EXCEPTION
     WHEN OTHERS THEN
     Rollback;
     v_err_descr := 'FATAL ERROR OCCURRED -'||cur_evt||sqlerrm;
     dbms_output.put_line (v_err_descr);
END;
FUNCTION numtohex(v_hex number) return varchar2
is
hex varchar2(4);
num1 number;
num2 number;
begin
num1 := trunc(v_hex/16);
num2 := v_hex-(num1*16);
if ( num1 >= 0 and num1 <= 9 ) then
hex := hex||to_char(num1);
end if;
if num1 = 10 then hex := hex||'A'; end if;
if num1 = 11 then hex := hex||'B'; end if;
if num1 = 12 then hex := hex||'C'; end if;
if num1 = 13 then hex := hex||'D'; end if;
if num1 = 14 then hex := hex||'E'; end if;
if num1 = 15 then hex := hex||'F'; end if;
if ( num2 >= 0 and num2 <= 9 ) then
hex := hex||to_char(num2);
end if;
if num2 = 10 then hex := hex||'A'; end if;
if num2 = 11 then hex := hex||'B'; end if;
if num2 = 12 then hex := hex||'C'; end if;
if num2 = 13 then hex := hex||'D'; end if;
if num2 = 14 then hex := hex||'E'; end if;
if num2 = 15 then hex := hex||'F'; end if;
return hex;
end;

Similar Messages

  • How to convert CLOB to BLOB with SQL Developer migration wizard?

    Hi,
    According to our requirement, we need to only migrate data from SQL Server 2008 to Oracle 11. I use the SQL Developer 3.0.04 migration wizard to do it.
    I do migration from SQL Server (database name: SCDS41P2) to Oracle (User name: HCDS41P2).
    My migration steps are as below: (I need to modify the target schema, so I add the steps 2) and step 3) )
    1) Do migration by SQL Developer migration wizard;
    2) Remove the Converted Database Objects node from migration project;
    3) Re-do convert by SQL Developer migration wizard (contains changing the target schema);
    When do step 1), the DBO_SCDS41P2 user is created automatically. CLOBTOBLOB_SQLDEVELOPER procedure is also created in this user.
    I continued to run step 2) and step 3) with online mode. And find the table data which contains BLOB column can be moved into the target table. But from the Logging Page, I can't see the other detailed error info, so that I can't check which tables don't move data successfully.
    Could you please tell me where the detail log file (e.g. contains every table's migration status) is if I use the online mode?
    Additional, If I use offline mode to do step 3), I found the CLOB data can't be converted into BLOB automatically. Because in the load script (that is oracle_ctl.bat), only copy the source data to the CLOB column. After copy, it don't deal with the converting from CLOB to BLOB.
    So could you please tell me how to convert CLOB to BLOB with offline mode?
    Thanks so much.

    Hi,
    For your first question about logging - after the migration there will be an entry in the right hand panel for the migration project. If you open this there are various fields that give information about the status of each part of the migration. Does this give you the information you need ?
    For the second problem about offline moving blob data have a look at the SQL*Developer documentation -
    Oracle® SQL Developer User’s Guide Release 3.0
    in the section -
    2.2.8.1.4 Populating the Destination Database Using the Data Files
    which describes the problem and how to get round it.
    Regards,
    Mike
    Edited by: mkirtley on Aug 12, 2011 10:49 AM

  • Converting CLOB to BLOB

    I am trying to convert clob into blob through following code.
    CREATE OR REPLACE FUNCTION clob_to_blob (clob_in IN CLOB)
    RETURN BLOB
    AS
    v_blob BLOB;
    v_varchar RAW(32767);
    v_start BINARY_INTEGER := 1;
    v_buffer BINARY_INTEGER := 32767;
    BEGIN
    FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(clob_in) / v_buffer)
    LOOP
    v_varchar := UTL_RAW.CAST_TO_RAW(DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start)) ;
    DBMS_OUTPUT.PUT_LINE('DATA :' ||DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start));
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR :'|| v_VARCHAR);
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR LENGTH :'|| LENGTH(v_VARCHAR));
    DBMS_LOB.WRITEAPPEND(v_blob, LENGTH(v_VARCHAR), v_varchar);
    DBMS_OUTPUT.PUT_LINE('after append');
    v_start := v_start + v_buffer;
    END LOOP;
    RETURN v_blob;
    END clob_to_blob;
    calling code:
    declare
    var clob;
    begin
    var:='3433534534de';
    testblob(var);
    end;
    It gives me following error:
    declare
    ERROR at line 1:
    ORA-21560: argument 2 is null, invalid, or out of range
    i don't know whats the prob?
    thanx

    dbms_lob.writeappend expects a real lob locator, which you can only get by selecting from a lob column.
    create table blobtab (b blob);
    insert into blobtab values (empty_blob());
    CREATE OR REPLACE FUNCTION clob_to_blob (clob_in IN CLOB)
    RETURN BLOB
    AS
    v_blob blob;
    v_varchar RAW(32767);
    v_start BINARY_INTEGER := 1;
    v_buffer BINARY_INTEGER := 32767;
    BEGIN
    select b into v_blob from blobtab;
    FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(clob_in) / v_buffer)
    LOOP
    v_varchar := UTL_RAW.CAST_TO_RAW(DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start)) ;
    DBMS_OUTPUT.PUT_LINE(' DATA :' || DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start));
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR :' || v_VARCHAR);
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR LENGTH :' || utl_raw.LENGTH(v_VARCHAR));
    DBMS_LOB.WRITEAPPEND(v_blob, utl_raw.LENGTH(v_VARCHAR), v_varchar);
    DBMS_OUTPUT.PUT_LINE('after append');
    v_start := v_start + v_buffer;
    END LOOP;
    RETURN v_blob;
    END clob_to_blob;
    declare
    blobvar blob;
    begin
    blobvar := clob_to_blob(dbms_xmlgen.getXML('select 1 from dual'));
    end;
    /

  • Converting CLOB to BLOB at once

    Hello all!
    I have to convert CLOB to BLOB in my application. My CLOB contains XML document built with dbms_xmldom, an I need to put it into column of type BLOB. Knowing several ways to do it, I'm searching for the best one. So I considered the folowing:
    - Size of XML data can be more then 4000 bytes.
    - I can convert it "piecewise", copying through varchar2 and raw buffers by chunks. But this is not efficient for me.
    - I can write CLOB to file, then read it into BLOB. But it is not efficient too.
    - I can not change column type to CLOB since it may contain other BLOB data, not XML.
    Actually, I need a way to "cast" CLOB data as binary data when inserting into table. I believe Oracle must have it, but where?
    I'll be very grateful for help from anyone.
    P.S. Using Oracle 9.2

    You could use UTL_RAW.CAST_TO_RAWhttp://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96612/u_raw2.htm#998330
    The XML data can be 32k (the varchar2 limit in PLSQL is 32k, not 4000 bytes)
    If your documents exceeds 32k you need process chunks

  • Converting varchar2 to BLOB or converting CLOB to BLOB

    Hi i am using a table where i have to convert a field currently existing as a varchar2 or CLOB to a BLOB field.
    while doing this i also have to preserve the data currently existing in the table.
    Please help.
    Thanks

    CLOB to a BLOB CREATE OR REPLACE FUNCTION clob_to_blob (clob_in IN CLOB) RETURN BLOB AS
    v_blob BLOB;
    v_varchar RAW(32001);
    v_varchar1 RAW(32001);
    v_start INTEGER := 1;
    v_buffer INTEGER := 32001;
    BEGIN
    DBMS_LOB.CREATETEMPORARY(v_blob, TRUE);
    FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(clob_in) / v_buffer) LOOP
    DBMS_LOB.READ(clob_in,v_buffer,v_start,v_varchar);
    DBMS_LOB.WRITEAPPEND(v_blob, utl_raw.length(v_varchar), v_varchar);
    v_start := v_start + v_buffer;
    END LOOP;
    RETURN v_blob;
    END clob_to_blob;
    /

  • Invalid data after converting clob to blob

    GOAL: insert adresse or someth. into an msword file out of database
    Hi,
    I'm working with a 9i database and I'm trying to insert data in a clob. First I convert the blob, which is a stored xml- file in my databse, to a clob. after that i'm replacing the data I want to insert and then reconverting to blob. It goes very well, but when the data I insert contains a 'ö' or 'ä' or 'ß' the blob file i get is invalid and can't be read by msword! I opened the file with notepad and instead of 'ö' etc there is a very strange sign like a square. here is my plsql- code:
    loop
    test:=substr(v_clob,posi,len);
    posi:=posi+32767;
    if length(v_clob)-posi<32767 then
    len:=length(v_clob)-posi+1;
    end if;
    --vartest:=utl_raw.cast_to_raw(v_clob);
    DBMS_LOB.WRITE(r_blob, length(test),posi-32767, utl_raw.cast_to_raw(test));
    exit when posi>length(v_clob);
    end loop;
    DBMS_LOB.freetemporary(v_clob);
    return r_blob;
    because of 9i doesn't contain the procedure dbms_lob.converttoblob i user utl_raw.cast_to_raw and split my clob in several parts with a size of 32767 characters
    i also tried with utl_raw.convert with several character sets, but don't know which one to take...
    nls_lang of database server is set to GERMAN_GERMANY.WE8MSWIN1252
    can anyone help me please!
    Message was edited by:
    user472439

    dbms_lob.writeappend expects a real lob locator, which you can only get by selecting from a lob column.
    create table blobtab (b blob);
    insert into blobtab values (empty_blob());
    CREATE OR REPLACE FUNCTION clob_to_blob (clob_in IN CLOB)
    RETURN BLOB
    AS
    v_blob blob;
    v_varchar RAW(32767);
    v_start BINARY_INTEGER := 1;
    v_buffer BINARY_INTEGER := 32767;
    BEGIN
    select b into v_blob from blobtab;
    FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(clob_in) / v_buffer)
    LOOP
    v_varchar := UTL_RAW.CAST_TO_RAW(DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start)) ;
    DBMS_OUTPUT.PUT_LINE(' DATA :' || DBMS_LOB.SUBSTR(clob_in, v_buffer, v_start));
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR :' || v_VARCHAR);
    DBMS_OUTPUT.PUT_LINE(' V_VARCHAR LENGTH :' || utl_raw.LENGTH(v_VARCHAR));
    DBMS_LOB.WRITEAPPEND(v_blob, utl_raw.LENGTH(v_VARCHAR), v_varchar);
    DBMS_OUTPUT.PUT_LINE('after append');
    v_start := v_start + v_buffer;
    END LOOP;
    RETURN v_blob;
    END clob_to_blob;
    declare
    blobvar blob;
    begin
    blobvar := clob_to_blob(dbms_xmlgen.getXML('select 1 from dual'));
    end;
    /

  • How to convert clob to blob

    hi
    tell me some thong how we can convert clob to blob

    You can use my procedure
    create or replace procedure CLOB2BLOB (p_clob in out nocopy clob, p_blob in out nocopy blob) is
    -- transforming CLOB &#226; BLOB
    l_off number default 1;
    l_amt number default 4096;
    l_offWrite number default 1;
    l_amtWrite number;
    l_str varchar2(4096 char);
    begin
    begin
    loop
    dbms_lob.read ( p_clob, l_amt, l_off, l_str );
    l_amtWrite := utl_raw.length ( utl_raw.cast_to_raw( l_str) );
    dbms_lob.write( p_blob, l_amtWrite, l_offWrite,
    utl_raw.cast_to_raw( l_str ) );
    l_offWrite := l_offWrite + l_amtWrite;
    l_off := l_off + l_amt;
    l_amt := 4096;
    end loop;
    exception
    when no_data_found then
    NULL;
    end;
    end;
    Best regards, Victor

  • LOB : how convert CLOB into BLOB

    This code doesn't work : ORA-06502: PL/SQL: numeric or value error: hex to raw conversion error.
    declare
    vCLOB CLOB := empty_clob();
    vBLOB BLOB := empty_blob();
    begin
    dbms_lob.createTemporary(vCLOB,TRUE);
    dbms_lob.open(vCLOB, DBMS_LOB.LOB_READWRITE);
    -- Put data into CLOB
    l_length := dbms_lob.getlength(vCLOB);
    dbms_lob.read(vCLOB,l_length,1,buffer);
    dbms_lob.createTemporary(vBLOB,TRUE);
    dbms_lob.open(vBLOB, DBMS_LOB.LOB_READWRITE);
    dbms_lob.write(vBLOB,l_length,1,buffer);
    dbms_lob.close(vBLOB);
    dbms_lob.freeTemporary(vBLOB);
    dbms_lob.close(vCLOB);
    dbms_lob.freeTemporary(vCLOB);
    Thanks.

    You can use my procedure
    create or replace procedure CLOB2BLOB (p_clob in out nocopy clob, p_blob in out nocopy blob) is
    -- transforming CLOB &#226; BLOB
    l_off number default 1;
    l_amt number default 4096;
    l_offWrite number default 1;
    l_amtWrite number;
    l_str varchar2(4096 char);
    begin
    begin
    loop
    dbms_lob.read ( p_clob, l_amt, l_off, l_str );
    l_amtWrite := utl_raw.length ( utl_raw.cast_to_raw( l_str) );
    dbms_lob.write( p_blob, l_amtWrite, l_offWrite,
    utl_raw.cast_to_raw( l_str ) );
    l_offWrite := l_offWrite + l_amtWrite;
    l_off := l_off + l_amt;
    l_amt := 4096;
    end loop;
    exception
    when no_data_found then
    NULL;
    end;
    end;
    Best regards, Victor

  • How to convert clobs to blobs?

    What is the easiest way to go if I want to copy the content of a CLOB to a BLOB(Other than reading the contents of the CLOB out into a buffer and then apply UTL_RAW.CAST_TO_RAW)? How about from blobs to clobs?
    null

    Hi,
    create table myblob(x blob);
    create table myblob1(x clob);
    create or replace procedure blob_to_clob as
    b_lob blob;
    buffer VARCHAR2(32000);
    g_length number;
    begin
    select x into b_lob from myblob;
    g_length := dbms_lob.getlength(b_lob);
    dbms_lob.read(b_lob,g_length,1,buffer);
    insert into myblob1 values(buffer);
    commit;
    end;
    null

  • How to convert a CLOB to BLOB

    Hi,
    Can any one tell me how to convert a CLOB into BLOB? In Oracle 10g there is a function which is converttoblob(). But in Oracle 9i there is no function as such. If i am using Hextoraw() function still then its giving some pointer error. please let me know the solution.

    FUNCTION c2b( c IN CLOB ) RETURN BLOB
    -- typecasts CLOB to BLOB (binary conversion)
    IS
    pos PLS_INTEGER := 1;
    buffer RAW( 32767 );
    res BLOB;
    lob_len PLS_INTEGER := DBMS_LOB.getLength( c );
    BEGIN
    DBMS_LOB.createTemporary( res, TRUE );
    DBMS_LOB.OPEN( res, DBMS_LOB.LOB_ReadWrite );
    LOOP
    buffer := UTL_RAW.cast_to_raw( DBMS_LOB.SUBSTR( c, 16000, pos ) );
    IF UTL_RAW.LENGTH( buffer ) > 0 THEN
    DBMS_LOB.writeAppend( res, UTL_RAW.LENGTH( buffer ), buffer );
    END IF;
    pos := pos + 16000;
    EXIT WHEN pos > lob_len;
    END LOOP;
    RETURN res; -- res is OPEN here
    END c2b;

  • Converting CHAR or CLOB to BLOB in Oracle9i

    Hi!
    I want to convert CHAR or CLOB to BLOB.
    I am working with Oracle9i Database.
    Oracle9i Supplied PL/SQL Package Release 2 (9.2) does'nt support DBMS_LOB.CONVERTTOBLOB procedure.
    How I can convert CHAR or CLOB to BLOB in Oracle9i?
    Valery

    Hi Valery,
    For conversion to blob, just try the utl_raw package.
    I use utl_raw.cast_to_raw quite often.
    Or have a look at:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:437819871174
    Good luck,
    Jan-Marcel

  • Can I convert existing column BLOB to CLOB?

    Hi ,
    I need conversion of existing column from BLOB to CLOB. Kindly sueggest workaround. Table structure is as
    { desc tab1;
    Name Null? Type
    Name Null? Type
    RECID NOT NULL VARCHAR2(255)
    XMLRECORD BLOB}
    I need to convert it from blob to clob. Kindly provide workaround.
    Regards

    Hi,
    How can i use this function for my scenario
    1. Table name is "test" and XMLRECORD column should be in CLOB without drop table. Kindly update this fuction as per my scenario and also provide all the steps
    CREATE OR REPLACE FUNCTION blob_to_clob (blob_in IN BLOB)
    RETURN CLOB
    AS
    v_clob CLOB;
    v_varchar VARCHAR2(32767);
    v_start PLS_INTEGER := 1;
    v_buffer PLS_INTEGER := 32767;
    BEGIN
    DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);
    FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(blob_in) / v_buffer)
    LOOP
    v_varchar := UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(blob_in, v_buffer, v_start));
    DBMS_LOB.WRITEAPPEND(v_clob, LENGTH(v_varchar), v_varchar);
    v_start := v_start + v_buffer;
    END LOOP;
    RETURN v_clob;
    END blob_to_clob;
    ]

  • BASE64 (PDF) CLOB to BLOB

    Hello everybody! I have to convert a BASE64 representation of a PDF file to BLOB. I found one example on the Internet but there is something wrong with it and I can't figure it out.
    create or replace function decode_base64(p_clob_in in clob) return blob is
    v_blob           blob;
    v_offset         integer;
    v_buffer_varchar varchar2(32000);
    v_buffer_raw     raw(32000);
    v_buffer_size    binary_integer := 32000;
    begin
    if p_clob_in is null then
    return null;
    end if;
    dbms_lob.CREATETEMPORARY(v_blob, true);
    v_offset := 1;
    FOR i IN 1..CEIL(dbms_lob.GETLENGTH(p_clob_in) / v_buffer_size)
    loop
    dbms_lob.READ(p_clob_in, v_buffer_size, v_offset, v_buffer_varchar);
    v_buffer_raw := utl_encode.BASE64_DECODE(utl_raw.CAST_TO_RAW(v_buffer_varchar));*
    dbms_lob.WRITEAPPEND(v_blob, utl_raw.LENGTH(v_buffer_raw), v_buffer_raw);
    v_offset := v_offset v_buffer_size;+
    end loop;
    return v_blob;
    end decode_base64;
    The above procedure should work with files of any size (since there is a loop), but it only works when the PDF is smaller than 32 KB.

    Just a guess.
    You may have additional CRLFs added when encoding.
    Try this:
    create or replace function replaceClob (opCLob CLOB,cpReplaceStr VARCHAR2,cpReplaceWith VARCHAR2) RETURN CLOB IS
       cBuffer    VARCHAR2 (32767);
       nBuffer    BINARY_INTEGER := 32767;
       nStart     PLS_INTEGER := 1;
       nLen            PLS_INTEGER;
       oCRtn      CLOB := EMPTY_CLOB;
       BEGIN
         DBMS_LOB.CreateTemporary(oCRtn,TRUE);
         nLen := DBMS_LOB.GetLength(opCLob);
         WHILE nStart < nLen
         LOOP
           DBMS_LOB.Read(opCLob, nBuffer, nStart, cBuffer);
           IF cBuffer IS NOT NULL THEN
             cBuffer := REPLACE(cBuffer, cpReplaceStr, cpReplaceWith);
             DBMS_LOB.WriteAppend(oCRtn, LENGTH(cBuffer), cBuffer);
           END IF;
           nStart := nStart + nBuffer;
         END LOOP;
         RETURN oCRtn;
       END;and then add to your code:
    create or replace function decode_base64(p_clob_in in clob) return blob is
           v_blob blob;
           v_offset integer;
           v_tem_clob clob;
           v_buffer_varchar varchar2(32000);
           v_buffer_raw raw(32000);
           v_buffer_size binary_integer := 32000;
           --v_buffer_size binary_integer := 3*1024;      
           begin
           if p_clob_in is null then
              return null;
           end if;
           -- Add this line
           v_tem_clob := replaceClob(p_clob_in,UTL_TCP.CRLF,'');
           dbms_lob.CREATETEMPORARY(v_blob, true);
           v_offset := 1;
           FOR i IN 1..CEIL(dbms_lob.GETLENGTH(v_tem_clob) / v_buffer_size) loop
               dbms_lob.READ(v_tem_clob, v_buffer_size, v_offset, v_buffer_varchar);          
               v_buffer_raw := utl_encode.BASE64_DECODE(utl_raw.CAST_TO_RAW(v_buffer_varchar));
               dbms_lob.WRITEAPPEND(v_blob, utl_raw.LENGTH(v_buffer_raw), v_buffer_raw);
               v_offset := v_offset + v_buffer_size;
           end loop;
           return v_blob;
    end decode_base64;Not tested.
    HTH
    Thomas

  • CLOB to BLOB replacing spaces with Â

    I have a CLOB that we are converting to a BLOB using DBMS_LOB.CONVERTTOBLOB and then serving it up as a file using WPG_DOCLOAD.DOWNLOAD_FILE. When you open the file, it is replacing the spaces with a Â.
    Any thoughts?

    Most likely, the spaces in the original CLOB were not regular spaces (0x20) but rather non-breaking spaces (0xA0).
    What's the target encoding used to convert to BLOB? I suppose UTF-8?
    UTF-8 encodes non-breaking spaces as 0xC2A0, where 0xC2 is specifically the codepoint for "Â". So I think the tool you're using doesn't support UTF-8, or doesn't recognize the file as being properly encoded.
    Edited by: odie_63 on 14 août 2012 14:26

  • Convert CLOB to LONG

    I have an app that has a CLOB column in a table. The reporting software, which is separate from the application, cannot read CLOBs. So the solution that was revealed to me was to setup a trigger to copy any data going into the CLOB table to another table where the column is a LONG.
    Of course, no DBA in their right mind would be happy with this. So, I'm trying to set about creating a view that will convert the CLOB to a LONG, so as to relieve my poor SAN from all the I/O. There is no TO_LONG function, nor can I find anywhere how to do this.
    Anyone feel like taking a stab at this?

    Tom,
    My guess is you might have to use dbms_lob.substr subprogram. Anyway, LONG datatype has a limit of 2gb while clob or blob can be bigger (theoritically upto 8TB ). So, you have to decide if you wanna truncate your data and store only the first 2gb. Also, just for information purposes, this is what Oracle says in documentation.
    ----------ORACLE DOCUMENTATION------
    Note:
    Do not create tables with LONG columns. Use LOB columns (CLOB, NCLOB) instead. LONG columns are supported only for backward compatibility.
    Oracle also recommends that you convert existing LONG columns to LOB columns. LOB columns are subject to far fewer restrictions than LONG columns. Further, LOB functionality is enhanced in every release, whereas LONG functionality has been static for several releases.
    HTH,
    Rahul.

Maybe you are looking for