Oracle UTL_RAW

来源:互联网 发布:科幻小说 知乎 编辑:程序博客网 时间:2024/06/05 16:55
General InformationSource{ORACLE_HOME}/rdbms/admin/utlraw.sqlFirst Available7.3.4ConstantsNameData TypeValuebig_endianPLS_INTEGER1little_endianPLS_INTEGER2machine_endianPLS_INTEGER3Dependencies179 objects

SELECT name FROM dba_dependencies
WHERE referenced_name = 'UTL_RAW'
UNION
SELECT referenced_name FROM dba_dependencies
WHERE name = 'UTL_RAW';
ExceptionsError #NameDescriptionVALUE_ERRORORA-6502An arithmetic, conversion, truncation, or size-constraint error. Usually raised by trying to cram a 6 character string into a VARCHAR2(5).Required Object PrivilegesGRANT execute on UTL_RAWGRANT execute ON utl_raw TO UWCLASS; BIT_ANDPerform bitwise logical "and" of the values in raw r1 with raw r2 and return the "anded" result rawutl_raw.bit_and(r1 IN RAW, r2 IN RAW) RETURN RAW;SELECT utl_raw.bit_and('0102F3', 'F30201')
FROM dual;
 BIT_COMPLEMENTPerform bitwise logical "complement" of the values in raw and return the "complement'ed" result rawutl_raw.bit_complement(r IN RAW) RETURN RAW;SELECT utl_raw.bit_complement('0102F3')
FROM dual;
 BIT_ORPerform bitwise logical "or" of the values in raw r1 with raw r2 and return the "or'd" result rawutl_raw.bit_or(r1 IN RAW, r2 IN RAW) RETURN RAW;SELECT utl_raw.bit_or('0102F3', 'F30201')
FROM dual;
 BIT_XORPerform bitwise logical "exclusive or" of the values in raw r1 with raw r2 and return the "xor'd" result rawutl_raw.bit_xor(r1 IN RAW, r2 IN RAW) RETURN RAW;SELECT utl_raw.bit_xor('0102F3', 'F30201')
FROM dual;
 CAST_FROM_BINARY_DOUBLEReturn the RAW representation of a binary_double valueutl_raw.cast_from_binary_double(n IN BINARY_DOUBLE,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_from_binary_double(123.45)
FROM dual;
 CAST_FROM_BINARY_FLOATReturn the RAW representation of a binary_float valueutl_raw.cast_from_binary_float(n IN BINARY_FLOAT,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_from_binary_float(123.45)
FROM dual;
 CAST_FROM_BINARY_INTEGERReturn the RAW representation of a binary_integer valueutl_raw.cast_from_binary_integer(
n         IN BINARY_INTEGER,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_from_binary_integer(100)
FROM dual;
 CAST_FROM_NUMBERReturns the binary representation of a NUMBER in RAWutl_raw.cast_from_number(n IN NUMBER) RETURN RAW;SELECT utl_raw.cast_from_number(100)
FROM dual;
 CAST_TO_BINARY_DOUBLEPerform a casting of the binary representation of the raw into a binary_double.cast_to_binary_double(
r         IN RAW,
endianess IN PLS_INTEGER DEFAULT 1) RETURN BINARY_DOUBLE;
SELECT utl_raw.cast_to_binary_double('405EDCCCCCCCCCCD')
FROM dual;
 CAST_TO_BINARY_FLOATPerform a casting of the binary representation of the raw into a binary_floatutl_raw.cast_to_binary_float(
n         IN BINARY_FLOAT,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_to_binary_float('42F6E666')
FROM dual;
 CAST_TO_BINARY_INTEGERPerform a casting of the binary representation of the raw into a binary integerutl_raw.cast_to_binary_integer(
r         IN RAW,
endianess IN PLS_INTEGER DEFAULT 1) RETURN BINARY_INTEGER;
SELECT utl_raw.cast_to_binary_integer('00000064')
FROM dual;
 CAST_TO_NUMBERPerform a casting of the binary representation of the number (in RAW) into a NUMBERutl_raw.cast_to_number(r IN RAW) RETURN NUMBER;SELECT utl_raw.cast_to_number('C202')
FROM dual;
 CAST_TO_NVARCHAR2
Converts a RAW represented using n data bytes into NVARCHAR2 with n data bytes
utl_raw.cast_to_nvarchar2(r IN RAW) RETURN NVARCHAR2;set serveroutput on

BEGIN
  FOR i IN 100..200 LOOP
    dbms_output.put_line(
    utl_raw.cast_to_nvarchar2(TO_CHAR(i)));
  END LOOP;
END;
/
 CAST_TO_RAWConverts a VARCHAR2 represented using n data bytes into a RAW with n data bytesutl_raw.cast_to_raw(r IN RAW) RETURN RAW;SELECT utl_raw.cast_to_raw('ABC')
FROM dual;
 CAST_TO_VARCHAR2To extract a substring from a BLOB using a PL/SQL program use dbms_lob.substr(). The problem is that it returns a string in hexadecimal characters. CAST_TO_VARCHAR2 turns the hexadecimal string into readable ascii format.utl_raw.cast_to_nvarchar2(r IN RAW) RETURN VARCHAR2;set serveroutput on

BEGIN
  FOR i IN 100..200 LOOP
    dbms_output.put_line(utl_raw.cast_to_varchar2(TO_CHAR(i)));
  END LOOP;
END;
/
 COMPARECompares raw r1 against raw r2. Returns 0 if r1 and r2 are identical, otherwise, returns the position of the first byte from r1 that does not match r2utl_raw.compare(r1 IN RAW, r2 IN RAW,
pad IN RAW DEFAULT NULL) RETURN NUMBER;
SELECT utl_raw.compare(utl_raw.cast_to_raw('ABC'),
utl_raw.cast_to_raw('ACC'))
FROM dual;
 CONCAT
Concatenate a set of 12 raws into a single raw (up to 32K)
utl_raw.concat(r1 IN RAW DEFAULT NULL,
r2  IN RAW DEFAULT NULL,
r3  IN RAW DEFAULT NULL,
r4  IN RAW DEFAULT NULL,
r5  IN RAW DEFAULT NULL,
r6  IN RAW DEFAULT NULL,
r7  IN RAW DEFAULT NULL,
r8  IN RAW DEFAULT NULL,
r9  IN RAW DEFAULT NULL,
r10 IN RAW DEFAULT NULL,
r11 IN RAW DEFAULT NULL,
r12 IN RAW DEFAULT NULL) RETURN RAW;
SELECT utl_raw.concat('A','41','B','42')
FROM dual;
 CONVERT
Convert raw from one character to a different character set and return the resulting raw
utl_raw.convert(
r            IN RAW,
to_charset   IN VARCHAR2,
from_charset IN VARCHAR2) RETURN RAW;
DECLARE
 fr_charset CONSTANT VARCHAR2(30) :=
 SUBSTR(SYS_CONTEXT('USERENV', 'LANGUAGE'),
 INSTR(SYS_CONTEXT('USERENV', 'LANGUAGE'),'.')+1);

 to_charset VARCHAR2(30) := 'TR8EBCDIC1026S';
 rawvar     RAW(100);
BEGIN
  dbms_output.put_line(fr_charset);
  dbms_output.put_line(to_charset);

  rawvar := utl_raw.convert(UTL_RAW.CAST_TO_RAW('Morgan'),
  'AMERICAN_AMERICA.'||to_charset,
  'AMERICAN_AMERICA.'||fr_charset);

  dbms_output.put_line(rawvar);
END;
/
 COPIESReturn n copies of r concatenated togetherutl_raw.copies(r IN RAW, n IN NUMBER) RETURN RAW;SELECT utl_raw.copies('A', 6)
FROM dual;
 LENGTHReturn the length in bytes of a rawutl_raw.length(r IN RAW) RETURN NUMBER;SELECT utl_raw.length('ABC')
FROM dual;
 OVERLAYOverlay the specified portion of target raw with overlay raw, starting from byte position pos of target and proceeding for len bytesutl_raw.overlay(overlay_str IN RAW, target IN RAW,
pos IN BINARY_INTEGER DEFAULT 1,
len IN BINARY_INTEGER DEFAULT NULL,
pad IN RAW DEFAULT NULL) RETURN RAW;
SELECT utl_raw.overlay('1', 'AAABBBCCC', 4)
FROM dual;
 REVERSEReverse a byte sequence in raw r from end to endutl_raw.reverse(r IN RAW) RETURN RAW;SELECT utl_raw.reverse('123')
FROM dual;
 SUBSTR
Return a substring portion of raw r beginning at pos for len bytes
utl_raw.substr(r IN RAW,
pos IN BINARY_INTEGER,
len IN BINARY_INTEGER DEFAULT NULL) RETURN RAW;
set serveroutput on

DECLARE
 tmp  VARCHAR2(250) := '';
 vraw RAW(200) := utl_raw.cast_to_raw('ABCDEABCDEABCDE');
BEGIN
  FOR i IN 1 .. 15 LOOP
    tmp := tmp|| '0x' || utl_raw.substr(vraw, i, 1) || ' ';
    dbms_output.put_line(tmp);
  END LOOP;
END;
/
 TRANSLATE
Translate the bytes in the input r raw according to the bytes in the translation raws, from_set and to_set
utl_raw.translate(r IN RAW, from_set IN RAW, to_set IN RAW)
RETURN RAW;
CREATE OR REPLACE FUNCTION trans_demo(pin IN VARCHAR2)
RETURN VARCHAR2 IS
 r_in  RAW(2000);
 r_out RAW(2000);
 r_ul  RAW(64);
 r_lu  RAW(64);
BEGIN
  r_in := utl_raw.cast_to_raw(pin);
  r_ul := utl_raw.cast_to_raw('ABCDEFabcdef');
  r_lu := utl_raw.cast_to_raw('abcdefABCDEF');

  r_out := utl_raw.translate(r_in , r_ul, r_lu);

  return(utl_raw.cast_to_varchar2(r_out));
END trans_demo;
/

SELECT trans_demo('FaDe') FROM dual;
SELECT trans_demo('FAde') FROM dual;
 TRANSLITERATE
Transliterate the bytes in the input r raw according to the bytes in the transliteration raws, from_set and to_set
utl_raw.transliterate(r IN RAW, to_set IN RAW DEFAULT NULL,
from_set IN RAW DEFAULT NULL, pad IN RAW DEFAULT NULL)
RETURN RAW;
CREATE OR REPLACE FUNCTION tl_demo(pin IN VARCHAR2)
RETURN VARCHAR2 IS
 r_in  RAW(2000);
 r_out RAW(2000);
 r_up  RAW(64);
 r_lo  RAW(64);
 r_un  RAW(64) := utl_raw.cast_to_raw('_');
BEGIN
  r_in := utl_raw.cast_to_raw(pin);
  r_up := utl_raw.cast_to_raw('ABCDEF. ');
  r_lo := utl_raw.cast_to_raw('abcdef');

  r_out := utl_raw.transliterate(r_in , r_lo, r_up, r_un);

  return(utl_raw.cast_to_varchar2(r_out));
END tl_demo;
/

SELECT tl_demo('AB C.D') FROM dual;
 XRANGE
Returns a raw containing all valid 1-byte encodings in succession beginning with the value start_byte and ending with the value end_byte.
utl_raw.xrange(start_byte IN RAW DEFAULT NULL,
end_byte IN RAW DEFAULT NULL) RETURN RAW;
SELECT utl_raw.xrange(utl_raw.cast_to_raw('A'),
utl_raw.cast_to_raw('Z'))
FROM dual;

SELECT utl_raw.xrange(utl_raw.cast_to_raw('0A'),
utl_raw.cast_to_raw('Z'))
FROM dual;
0 0