Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an elegant way to return a readable 'file size' of a file stored in an oracle blob column using SQL?

Tags:

sql

oracle

plsql

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

like image 213
Brian Avatar asked May 08 '09 17:05

Brian


2 Answers

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
like image 61
Brian Avatar answered Nov 02 '22 23:11

Brian


SELECT DBMS_LOB.GETLENGTH(COLUMN_NAME) FROM DOCUMENTS
like image 41
dpbradley Avatar answered Nov 03 '22 01:11

dpbradley