tags:

views:

248

answers:

2

How would you write a SQL query to get the size of blob in a more human readable form? The example would be something like a large word document is being stored in a blob on a table. I would want to execute something like:

select fnc_getReadableSize(documents.doc) from documents where id = ?

output: 23.4 MB

+4  A: 

SELECT DBMS_LOB.GETLENGTH(COLUMN_NAME) FROM DOCUMENTS

dpbradley
I should have emphasized the 'readable' part. I ended up using a WITH clause to establish my constants and then arbitrarily decided that KB and MB variations would be sufficient for my immediate one report need.
Brian
+2  A: 

The 'readable' part is what I should have emphasized more. Here is what I put together for now.

WITH file_sizes AS
     (SELECT 1048576 MEGABYTE, 1024 KILOBYTE,
             DBMS_LOB.GETLENGTH (BLOB_COLUMN) byte_size
        FROM BLOB_COLUMN)
SELECT (CASE TRUNC (byte_size / MEGABYTE)
           WHEN 0
              THEN TO_CHAR ((byte_size / KILOBYTE), '999,999') || ' KB'
           ELSE TO_CHAR ((byte_size / MEGABYTE), '999,999.00') || ' MB'
        END
       ) display_size
  FROM file_sizes


Output:

DISPLAY_SIZE  
--------------
       1.88 MB
     433 KB   
     540 KB   
     333 KB   
       1.57 MB
       1.17 MB
Brian