Convert from hex-encoded CLOB to BLOB in Oracle

南楼画角 提交于 2019-12-12 17:55:39

问题


I have some large binary content in hex form stored in a CLOB and want to convert that to a BLOB where the hex code is actual binary byte encoding:

DECLARE

  -- This would be my 8 byte hex-encoded binary content. Real content is much bigger
  c CLOB := 'cafebabe12345678';
  b BLOB;
BEGIN

  -- Need the implementation of this function
  b := hex_to_blob(c);
END;
/

What's the easiest way to do that in Oracle, using PL/SQL?


回答1:


The desired function could look like this:

CREATE OR REPLACE
FUNCTION hex_to_blob (hex CLOB) RETURN BLOB IS
  b BLOB                := NULL;
  s VARCHAR2(4000 CHAR) := NULL;
  l NUMBER              := 4000;
BEGIN
  IF hex IS NOT NULL THEN
    dbms_lob.createtemporary(b, FALSE);

    FOR i IN 0 .. LENGTH(hex) / 4000 LOOP
      dbms_lob.read(hex, l, i * 4000 + 1, s);
      dbms_lob.append(b, to_blob(hextoraw(s)));
    END LOOP;
  END IF;

  RETURN b;
END hex_to_blob;


来源:https://stackoverflow.com/questions/42002816/convert-from-hex-encoded-clob-to-blob-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!