UTL_ENCODE.BASE64_ENCODE Returns Different Results In SQL And PL/SQL (Doc ID 1332488.1)

Last updated on JULY 05, 2017

Applies to:

PL/SQL - Version: 10.2.0.5 to 11.2.0.2 - Release: 10.2 to 11.2
Information in this document applies to any platform.

Goal

Why does utl_encode.base64_encode produce different results in PLSQL and SQL?

(UTL_ENCODE is used when you have binary data that you want to transfer across the network. If you just stream this in it's raw format some protocols may interpret the binary data as control characters or character combinations and may act on this. To get around this, the binary data is encoded into characters using UTL_ENCODE.)

For example, from PL/SQL:

CREATE OR REPLACE FUNCTION TestEncodeBase64 (email nvarchar2) RETURN nvarchar2 IS
  result nvarchar2(256);
BEGIN
  SELECT UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.BASE64_ENCODE(UTL_RAW.CAST_TO_RAW(email)))
  INTO result
  FROM dual;
  RETURN result;
END TestEncodeBase64;
/

SQL> select TestEncodeBase64 ('TEST') from dual;

TESTENCODEBASE64('TEST')
-----------------------------------------
AFQARQBTAFQ=

From SQL:

SELECT UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.BASE64_ENCODE(UTL_RAW.CAST_TO_RAW('TEST'))) as test
FROM dual;

TEST
-----------------
VEVTVA==

Solution

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms