All Products
Search
Document Center

RAW data type

Last Updated: Jun 18, 2021

The RAW data type stores variable-length binary data. The data is transferred in binary mode across different platforms. The binary data does not need to be converted even if the platforms use different character sets. In ApsaraDB for OceanBase, this data type is used to store binary data or byte strings.

RAW is similar to VARCHAR2. The RAW data type is declared by using RAW(length). The length parameter specifies the value length in bytes. A value in a database column of the RAW data type can be up to 2,000 bytes. A variable of the RAW data type can be up to 2,000 bytes.

Syntax

RAW(length)

Parameters

Parameter

Description

length

The length of the value. The length is measured in bytes. A value in a database column of the RAW data type can be up to 2,000 bytes. A variable of the RAW data type can be up to 2,000 bytes.

Examples

  • Example 1: Assume that you declare a column of the RAW data type in the test_raw table and insert a record into the table.

CREATE TABLE test_raw (c1 RAW(10));
INSERT INTO test_raw VALUES (utl_raw.cast_to_raw('1234567890'));

Execute the following statement:

SELECT utl_raw.cast_to_varchar2(c1) FROM test_raw;

The following result is returned:

+-------------------------------+
|    UTL_RAW.CAST_TO_RAW(C1)    |
+-------------------------------+
|          1234567890           | 
+-------------------------------+
  • Example 2: Assume that you insert two records into the raw_test table.

CREATE TABLE raw_test (id number, raw_date raw(10));
INSERT INTO raw_test VALUES  (1, hextoraw('ff'));
INSERT INTO raw_test VALUES  (2, utl_raw.cast_to_raw('051'));

Execute the following statement:

SELECT * FROM raw_test;

The following result is returned:

+-----------+----------+
|    ID     | RAW_DATE |
+-----------+----------+
|      1    |     ff   |
+-----------+----------+
|      2    |   303531 |
+-----------+----------+

The SQL function HEXTORAW() converts the data in a string to a hexadecimal value. The SQL function UTL_RAW.CAST_TO_RAW([VARCHAR2]) stores the ASCII code of each character in a string into a RAW field. In this example, 051 is converted to 303531.

  • Example 3: ApsaraDB for OceanBase converts a value of the RAW type to a value of a character data type. Each character in the result represents the hexadecimal value for four consecutive bits in the value of the RAW type. The hexadecimal values can be digits 0 to 9 and letters A to F (or a to f).

For example, the binary number 11001011 of the RAW data type is converted to the CB string.

To convert character data to a RAW value, ApsaraDB for OceanBase interprets each consecutive input character as four consecutive bits of binary data. Then, ApsaraDB for OceanBase builds the resulting RAW value by concatenating those bits.

Notice

If an input character is not a hexadecimal value, an error is reported. Hexadecimal values include digits 0 to 9 and letters A to F (or a to f). If the number of characters is odd, the result is undefined.

More information

Character conversion

When data is transferred between databases or between the database character set and the client character set, ApsaraDB for OceanBase automatically converts the CHAR and VARCHAR2 data across database character sets. When the RAW data is transferred, ApsaraDB for OceanBase does not convert the characters.

RAW functions

Function

Description

HEXTORAW()

HEXTORAW() converts the data in a string to a hexadecimal string. Every two characters in the source string corresponds to 1 byte of the resulting RAW value.

RAWTOHEX(rawvalue)

This function converts rawvalue of the RAW type to a hexadecimal string.

Each byte of rawvalue is converted to a double-byte string.

UTL_RAW_CAST_TO_RAW([VARCHAR2])

This function only converts the data type from VARCHAR2 to RAW. The content stored in the data remains unchanged.

UTL_RAW_CAST_TO_VARCHAR2([RAW])

This function only converts the data type from RAW to VARCHAR2. The content stored in the data remains unchanged.

UTL_RAW.BIT_OR(), UTL_RAW.BIT_AND(), and UTL_RAW.BIT_XOR()

Bitwise operation.