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;
/
Similar Messages
-
hi
tell me some thong how we can convert clob to blobYou can use my procedure
create or replace procedure CLOB2BLOB (p_clob in out nocopy clob, p_blob in out nocopy blob) is
-- transforming CLOB â 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 â 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 call CLOB to BLOB conversion function within stored procedure?
I have a tiny APEX application from which I need to be able to print. I’ve used these two resources: (Is there an inexpensive APEX report printer for invoices/checks/statements? and http://download.oracle.com/docs/cd/E14373_01/appdev.32/e13363/up_dn_files.htm#CJAHDJDA)
I guess that in order to be able to download the RTF document stored in CLOB, I need to convert it into BLOB. I’ve found this function for this purpose on Oracle forums:
CREATE OR REPLACE 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;And I am trying to use it in the modified download procedure that I also have found on Oracle forums:
CREATE OR REPLACE PROCEDURE DOWNLOAD_WO(v_id IN NUMBER)
AS
v_mime VARCHAR2(48);
v_length NUMBER;
v_file_name VARCHAR2(2000):= 'WO_Download.rtf';
lob_loc CLOB;
v_blob BLOB;
v_company jobs_vw.company%TYPE;
v_project jobs_vw.project%TYPE;
v_description jobs_vw.description%TYPE;
v_date_ jobs_vw.date_%TYPE;
v_job_no jobs_vw.job_no%TYPE;
v_apqwo jobs_vw.apqwo%TYPE;
-- v_mime VARCHAR2(48) := 'application/msword';
BEGIN
SELECT mime_type, report, DBMS_LOB.GETLENGTH(report)
INTO v_mime,lob_loc,v_length
FROM report_layouts
WHERE rl_id = 22332925279634283;
-- JOB_VW record:
SELECT job_no
,date_
,description
,apqwo
,project
,company
INTO v_job_no
,v_date_
,v_description
,v_apqwo
,v_project
,v_company
FROM jobs_vw
WHERE id = 214;
-- Replace holders with actual values:
lob_loc := REPLACE(lob_loc, '#COMPANY#', v_company);
lob_loc := REPLACE(lob_loc, '#PROJECT#', v_project);
lob_loc := REPLACE(lob_loc, '#DESCRIPTION#', v_description);
lob_loc := REPLACE(lob_loc, '#DATE_#', TO_CHAR(v_date_, 'DD/MM/YYYY'));
lob_loc := REPLACE(lob_loc, '#JOB_NO#', v_job_no);
lob_loc := REPLACE(lob_loc, '#APQWO#', v_apqwo);
-- set up HTTP header
-- use an NVL around the mime type and
-- if it is a null set it to application/octect
-- application/octect may launch a download window from windows
owa_util.mime_header( nvl(v_mime,'application/octet'), FALSE );
-- set the size so the browser knows how much to download
htp.p('Content-length: ' || v_length);
-- the filename will be used by the browser if the users does a save as
htp.p('Content-Disposition: attachment; filename="'||replace(replace(substr(v_file_name,instr(v_file_name,'/')+1),chr(10),null),chr(13),null)|| '"');
-- close the headers
owa_util.http_header_close;
-- download the BLOB
-- wpg_docload.download_file( Lob_loc );
v_blob := c2b(lob_loc);
wpg_docload.download_file( v_blob );
end DOWNLOAD_WO;
/Unfortunately when I try to compile the download_wo stored procedure I am getting this error:
Error at line 64: PL/SQL: Statement ignoredThe 64th line is:
v_blob := c2b(lob_loc);How should I correctly call c2b within download_wo? Any advice is greatly appreciated.
Thank you for your time.
DanielHello there,
Well, its invalid :(
Object C2B is Invalid. I didn't know since when I run the create ... function, I only get this feedback:
Statement processed.
0.19 secondsI am investigating.
Daniel -
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; -
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.2You 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 -
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 -
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:
user472439dbms_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;
/ -
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?
ValeryHi 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 -
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?
thanxdbms_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 blob data into clob using plsql
hi all,
I have requirement to convert blob column into clob .
version details
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
DECLARE
v_blob temp.blob_column%TYPE;------this is blob data type column contains (CSV file which is inserted from screens)
v_clob CLOB; --i want to copy blob column data into this clob
v_warning NUMBER;
BEGIN
SELECT blob_column
INTO v_blob
FROM temp
WHERE pk = 75000676;
DBMS_LOB.converttoclob (dest_lob => v_clob,
src_blob => v_blob,
amount => DBMS_LOB.lobmaxsize,
dest_offset => 1,
src_offset => 1,
blob_csid => 1, -- what is the use of this parameter
lang_context => 1,
warning => v_warning
DBMS_OUTPUT.put_line (v_warning);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
DBMS_OUTPUT.put_line (SQLERRM);
END;I am not getting what is the use of blob_csid , lang_context parameters after going the trough the documentation .
Any help in this regard would be highly appreciated .......
Thanks
Edited by: prakash on Feb 5, 2012 11:41 PMPost the 4 digit Oracle version.
Did you read the Doc for DBMS_LOB.CONVERTTOCLOB? - http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_lob.htm
The function can convert data from one character set to another. If the source data uses a different character set than the target you need to provide the character set id of the source data.
The blob_csid parameter is where you would provide the value for the source character set.
If the source and target use the same character set then just pass zero. Your code is passing a one.
>
General Notes
You must specify the character set of the source data in the blob_csid parameter. You can pass a zero value for blob_csid. When you do so, the database assumes that the BLOB contains character data in the same character set as the destination CLOB.
>
Same for 'lang_context' - your code is using 1; just use 0. It is an IN OUT
>
lang_context
(IN) Language context, such as shift status, for the current conversion.
(OUT) The language context at the time when the current conversion is done.
This information is returned so you can use it for subsequent conversions without losing or misinterpreting any source data. For the very first conversion, or if do not care, use the default value of zero. -
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 -
BLOB-- CLOB-- BLOB conversion reducing length of BLOB
Hi,
I am using the following BLOB-->CLOB and CLOB-->BLOB conversion functions :
CREATE OR REPLACE FUNCTION blob_to_clob (blob_in IN BLOB)
RETURN CLOB
AS
v_clob CLOB;
v_varchar RAW(32001);
v_varchar1 VARCHAR2(32001);
v_start INTEGER := 1;
v_buffer INTEGER := 32001;
BEGIN
DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);
FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(blob_in) / v_buffer)
LOOP
DBMS_LOB.READ(blob_in,v_buffer,v_start,v_varchar);
DBMS_LOB.WRITEAPPEND(v_clob, utl_raw.length(v_varchar), v_varchar);
v_start := v_start + v_buffer;
END LOOP;
RETURN v_clob;
END blob_to_clob;
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;
I am using these functions to convert image files, stored as BLOB to CLOB for intermediate storage, and then converting them back to BLOB. In this process, I see a loss of information when the image is converted from CLOB to BLOB ...which is seen by using dbms_lob.getlength() .Using the following sql query:
select dbms_lob.getlength(file_body),
dbms_lob.getlength(blob_to_clob(file_body)),
dbms_lob.getlength(clob_to_blob(blob_to_clob(file_body)))
from my_table where image_id in (8819)
i see that returned length is usually half in the 3rd column, i.e. for dbms_lob.getlength(clob_to_blob(blob_to_clob(file_body))). There is no change in length for 1st and 2nd columns. Could you please let me know why length is getting lost in the CLOB to BLOB conversion ? Thanks.Thanks for replying.
This is required because we have to store images in ldt files using FNDLOAD, whiich only supports CLOB, not BLOB. I understand that images stored in binary formats should be stored in BLOB only, but I am exploring the possibility of converting BLOB format to CLOB and then back to BLOB without loss of information. Any pointers in this regard are appreciated. Thanks ! -
Conversion of BLOB datatype to CLOB datatype
Hi,
I would need to convert the column datatype from BLOB to CLOB. currently in the table, the BLOB column has the data. the requirement is to convert this column from BLOB to CLOB datatype.
please help me how to convert from BLOB datatype to CLOB datatype
Thanks
Hari.I have just been dealing with the same issue -- mass conversion of data in BLOB form to CLOB. I think I've finally got it working well. I have a table that has an key column then both a BLOB and a CLOB column. I load the BLOBS into the table then run the following PLSQL block (uses the given proc) to transfer the data from the BLOB column to the CLOB column.
-- Proc to convert BLOB to CLOB
create or replace function stg.blob_to_clob( p_blb in blob ) return clob is
v_clb clob ;
v_dst integer := 1 ;
v_src integer := 1 ;
v_wrn integer ;
v_lng integer := dbms_lob.default_lang_ctx ;
begin
dbms_lob.createtemporary ( v_clb, false ) ;
dbms_lob.converttoclob
( dest_lob => v_clb
, src_blob => p_blb
, amount => dbms_lob.lobmaxsize
, dest_offset => v_dst
, src_offset => v_src
, blob_csid => dbms_lob.default_csid
, lang_context => v_lng
, warning => v_wrn
if ( dbms_lob.NO_WARNING != v_wrn ) then
v_clb := '~~~{ Error: Invalid Character in source BLOB }~~~' ;
end if ;
return v_clb ;
exception
when others then
v_clb := '~~~{ Error: BLOB Conversion Function Failed }~~~' ;
return v_clb ;
end ;
-- Use the proc above to convert each BLOB to a CLOB in the given table.
declare
cursor c1 is
/* Select the BLOBS to be converted. */
select id, m_blob, m_clob
from stg.temp_cnvrt
/* Trying to convert NULL BLOBS will result in an error */
where dbms_lob.getlength(m_blob) > 0
for update ;
v_tmp_clob clob ;
begin
FOR nxt in c1 LOOP
v_tmp_clob := stg.blob_to_clob( nxt.m_blob ) ;
update stg.temp_cnvrt
set m_clob = v_tmp_clob
where current of c1 ;
END LOOP ;
commit ;
end ;
Maybe you are looking for
-
Does BI Beans support Asymmetric Reports?
Does BI Beans support assymetric reports, or have any plans to do so?
-
Plant Maintenance and Service Notification
WOuld like to ask where in SPRO to correct error in notification "Functional location field must be filled up". This applies to all notification types created by when I checked on SET FIELD FOR NOTIFICATIONS >Field Selection Reference Objects PM Noti
-
Driver Details of PC's reporting to SCCM
Hello Guys, Can you please help me with SQL Query to get the driver version details installed on PC? Thanks in Advance :) Mukhyaprana
-
NEXUS 5020: Power redudancy.
Hi, I want to configure nexus 5020 switch in power redudant mode. Please tell me the procedure and commands (with other options, like ps-redudant / grid). I would also like to know on the fan redudancy features. If any body can help me in getting a g
-
I'm trying to install KB2870703 however I have our servers setup this way: Server A: FIM Service & Sync Service Server B: SharePoint 2013, Password Reset Portal, Password Registration Portal When attempting to install FIMService_x64_KB2870703.msp It