Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How can I convert a blob to base64?

Hi,
I have a function that converts blob to base64, but the problem is that it seems that the function has a limit of characters, it is the following:

CREATE OR REPLACE FUNCTION BLOB_TO_BASE64(

                          P_BLOB BLOB

                         )

    RETURN CLOB

    IS

        V_CLOB CLOB;

        V_CHUNK_SIZE PLS_INTEGER := 24000;

    BEGIN

        FOR V_I IN 0..TRUNC((DBMS_LOB.GETLENGTH(P_BLOB) - 1 ) / V_CHUNK_SIZE) LOOP

          V_CLOB := V_CLOB || UTL_RAW.CAST_TO_VARCHAR2(

                                                       UTL_ENCODE.BASE64_ENCODE(

                                                                                DBMS_LOB.SUBSTR(

                                                                                                P_BLOB,

                                                                                                V_CHUNK_SIZE,

                                                                                                V_I * V_CHUNK_SIZE + 1

                                                                                               )

                                                                               )
                                                      );
        END LOOP;

        RETURN V_CLOB;

END;

When I run it with a small blob it works, but I try it with a PDF and I get the following error:

SELECT content_type, BLOB_TO_BASE64(CONTENT)  FROM syw_ticket_file WHERE file_name = 'test (1).pdf'


ORA-06502: PL/SQL: numeric or value error: raw variable length too long

And when I consult, the service is as follows:

555 User Defined Resource Error
The request could not be processed because an error occurred whilst attempting to evaluate the SQL statement associated with this resource. Please check the SQL statement is correctly formed and executes without error. SQL Error Code: 6502, Error Message: ORA-06502: PL/SQL: numeric or value error: raw variable length too long ORA-06512: at "SYS.UTL_ENCODE", line 243 ORA-06512: at "SYWORK.BLOB_TO_BASE64", line 10

I hope you can help me.

Comments

Post Details

Added on May 27 2022
5 comments
8,021 views