Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle database: How to read a BLOB?

Tags:

sql

oracle

blob

I'm working with an Oracle database, and I would like to read the contents of a BLOB. How do I do this?

When I do a simple select statement, it merely returns "(BLOB)" (without the quotes). How do I read the actual contents?

like image 880
ktm5124 Avatar asked Apr 15 '11 21:04

ktm5124


People also ask

How do I convert BLOB data to readable format?

If you want to update the BLOB datatype column to the TEXT datatype column. Follow these steps: Alter the table and add a column having data type TEXT. Add content to that column after converting BLOB data to TEXT date.

Where are BLOBs stored in Oracle?

BLOBs are not stored in the normal database files on disk in the same way as is other data managed by DB. Instead, they are stored as binary files in a special directory set aside for the purpose.

What is a BLOB in Oracle?

A BLOB (binary large object) is a varying-length binary string that can be up to 2,147,483,647 characters long.


1 Answers

You can dump the value in hex using UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2()).

SELECT b FROM foo;
-- (BLOB)

SELECT UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2(b))
FROM foo;
-- 1F8B080087CDC1520003F348CDC9C9D75128CF2FCA49D1E30200D7BBCDFC0E000000

This is handy because you this is the same format used for inserting into BLOB columns:

CREATE GLOBAL TEMPORARY TABLE foo (
    b BLOB);
INSERT INTO foo VALUES ('1f8b080087cdc1520003f348cdc9c9d75128cf2fca49d1e30200d7bbcdfc0e000000');

DESC foo;
-- Name Null Type 
-- ---- ---- ---- 
-- B        BLOB 

However, at a certain point (2000 bytes?) the corresponding hex string exceeds Oracle’s maximum string length. If you need to handle that case, you’ll have to combine How do I get textual contents from BLOB in Oracle SQL with the documentation for DMBS_LOB.SUBSTR for a more complicated approach that will allow you to see substrings of the BLOB.

like image 144
andrewdotn Avatar answered Oct 22 '22 02:10

andrewdotn