0

I needs to export Oracle BLOB column data to csv or text file.

select Blob1 from table it gives me in binary format.

Please give me the solution to export BLOB data to either csv or text file in normal readable format.

1
  • @Najdu5, I updated my answer to show you how to get all the content from the function in sqlplus by putting set long 99999999 Commented Aug 5, 2020 at 11:53

3 Answers 3

2

Normally BLOB datatypes are used for storing binary objects, such as images, videos, etc. However, if you have a BLOB that contains characters instead of binary data, then you can do the following:

1.Creates a function to return the BLOB into a CLOB

create or replace function blob_to_char (b blob)
return clob is
  v_clob    clob;
  n         number;
  v_start   pls_integer := 1;
  v_buffer  pls_integer := 32767;
  v_varchar varchar2(32767);
begin
  if (b is null) 
  then
    return null;
  end if;
  if (dbms_lob.getlength(b)=0) 
  then
    return empty_clob();
  end if;
  dbms_lob.createtemporary(v_clob,true);
  for i in 1..ceil(dbms_lob.getlength(b) / v_buffer)
  loop
    v_varchar := utl_raw.cast_to_varchar2(dbms_lob.substr(b, 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_char;

2.Then you can use the function in any select statement to recover your blob as a character string. Obviously, I am assuming the BLOB is in reality a string.

select column1, column2 , blob_to_char(your_blob_column) from your table ;

Example

SQL> create table t ( c1 number, c2 blob );

Table created.


SQL> insert into t values ( 1 , utl_raw.cast_to_raw('ppppppppppppppppppppppppppdoekkkkkkkkkkkkkkkkkkffj') ) ;

1 row created.

SQL> commit;

SQL> create or replace function blob_to_char
  2  ( b blob)
return clob is
  v_clob    clob;
  n         number;
  v_start   pls_integer := 1;
  v_buffer  pls_integer := 32767;
  v_varchar varchar2(327  3  67);
begin
  if (b is null)
  then
  4    5    6    7    8    9   10   11   12      return null;
  end if;
 13   14    if (dbms_lob.getlength(b)=0)
  then
    return empty_clob();
 15   16   17    end if;
  dbms_lob.createtemporary(v_clob,true);
  for i in 1..ceil(dbms_lob.getlength(b) / v_buffer)
  loop
        v_varchar := utl_raw.cast_to_varchar2(dbms_lob.substr(b, v_buffer, v_start));
 18   19   20   21   22         dbms_lob.writeappend(v_clob, length(v_varchar), v_varchar);
        v_start := v_start + v_buffer;
  end loop;
RETURN v_clob;
 23   24   25   26  end;
 27  /

Function created.

SQL> select c1 , blob_to_char(c2) from t ;

        C1
----------
BLOB_TO_CHAR(C2)

--------------------------------------------------------------------------------
         1
ppppppppppppppppppppppppppdoekkkkkkkkkkkkkkkkkkffj

Nevertheless, is a very bad practice to store huge text or strings using BLOB, for that you must use always CLOB, which is was it's intended for.

Update

If your BLOB is a very big, try to increase the output by

 SQL> SET LONG 99999999
 SQL> SELECT blob_to_char(blobcolum) from yourtable ;
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the help but My blob contains binary data which I wants to export to csv file in readable format.
what do you have stored in the BLOB ?? if it is a picture , there is no way. If it is a string stored as blob, use the answer I provided you
Hi @Roberto hernandez, Its blob field contains only xml data as it is string and the above function is working fine but its does not export content inside the xml tabs.
can you post an example of what the function does not export of the clob column ?
For example xml containes main tab with <?xml version encoding utf-8?> <Product message xmlns urn:product: and until here it is exporting but after then <msg type tab <application tab category> <profile tab some another tabs are there and which is not exporting with above function, please let me know is this limitation of the size or how to retrieve full data of blobxml binary data to string data.
|
0

You can't. BLOB is a Binary Large Object. It can contain e.g. images, music files, videos, ...

How do you plan to export a movie as a CSV or text file? What would you do with it, then?

Think it over, as it seems that you got something wrong.

1 Comment

BLOB column containes xml data in binary format and which I wants to export to any file format with readable format.
0

Thanks Roberto it works like a charm. SQL> SET LONG 99999999 SQL> SELECT blob_to_char(blobcolum) from yourtable ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.