DBA Tips Archive for Oracle

  


Viewing ASCII Data within a BLOB

by Jeff Hunter, Sr. Database Administrator

The following procedure can be used to retrieve the first 2000 bytes of data from a BLOB column and view all printable characters. Keep in mind that SQL functions do not work on the RAW data type. The procedure therefore needs to use the UTL_RAW package. If you need to install UTL_RAW use the following:

Procedure: readBLOB
CREATE OR REPLACE FUNCTION readBLOB (p_raw IN BLOB)
  RETURN VARCHAR2
  AS

  l_tmp     LONG     DEFAULT utl_raw.cast_to_varchar2(dbms_lob.substr(p_raw,2000,1));
  l_char    CHAR(1);
  l_return  LONG;

BEGIN

  FOR i IN 1 .. length(l_tmp)
  LOOP

    l_char := substr(l_tmp, i, 1);
    IF ( ascii(l_char) BETWEEN 32 and 127) THEN
      l_return := l_return || l_char;
    ELSE
      l_return := l_return || '.';
    END IF;

  END LOOP;
  RETURN l_return;

END;
/

show errors

The following query utilizies the above example. The "raw" data is being turned into into VARCHAR2 to SQL*Plus will print it.

Example query
SELECT
    obj_id_no obj_id
  , DBMS_LOB.getLength(obj_value) obj_length
  , readBLOB(obj_value) obj_value
FROM java_objects
/
Output
    OBJ_ID OBJ_LENGTH
---------- ----------
OBJ_VALUE
--------------------------------------------------------------------------------
         1        185
....sr..java.util.LinkedList.)S]J`."...xpw.....t..This is a testsr..java.lang.Lo
ng;.....#....J..valuexr..java.lang.Number...........xp.....[..sr..java.util.Date
hj..KYt....xpw......z.}xx


Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Monday, 01-Jan-2001 00:00:00 EST
Page Count: 12568