OceanBase Database supports implicit conversion between the XMLType and VARCHAR data types.
Note that we do not recommend that you use XMLType as a string. OceanBase Database stores XMLType data in binary format. This format optimizes XPath queries and removes the original formatting information of the input, such as the line breaks, indentation, and ignorable whitespace of XML elements. If you want to adjust the output format of XMLType data, you can use the XMLSERIALIZE() function.
The conversion rules between the XMLType data type and other data types are as follows:
| Source type | Target type | Implicit conversion | Explicit conversion |
|---|---|---|---|
| XMLType | VARCHAR2 | XMLType data can be inserted into a VARCHAR2 column and can be processed by functions such as SUBSTR(). |
Supported. You can use CAST(XMLType as VARCHAR2(n)) or XMLSERIALIZE(). |
| XMLType | CLOB | Not supported. | Not supported. However, you can use the XMLSERIALIZE() function to convert XMLType data to the CLOB type. |
| XMLType | BLOB | Not supported. | Not supported. However, you can use the XMLSERIALIZE() function to convert XMLType data to the BLOB type. |
| XMLType | Other basic types | Not supported. | Not supported. However, you can use the XMLCAST() function to convert text in an XML document to other basic types. |
| VARCHAR2 | XMLType | You can directly insert VARCHAR2 data into an XMLType column. However, you cannot directly use functions that take XMLType as an input parameter, such as EXTRACTVALUE(). |
Not supported. |
| CLOB | XMLType | Not supported. | Not supported. |
| BLOB | XMLType | Not supported. | Not supported. |
| Other basic types | XMLType | Not supported. | Not supported. |
The XMLType data type can be implicitly or explicitly converted to the VARCHAR, VARCHAR2, NVARCHAR2, and CHAR data types by using the CAST function. However, you cannot convert these data types to the XMLType data type by using the CAST() function. Here are some conversion examples:
# Explicit conversion examples
SELECT CAST(XMLPARSE(DOCUMENT '<a>123</a>') AS VARCHAR(100)) FROM DUAL;
SELECT CAST(XMLPARSE(DOCUMENT '<a>123</a>') AS VARCHAR2(100)) FROM DUAL;
SELECT CAST(XMLPARSE(DOCUMENT '<a>123</a>') AS NVARCHAR(100)) FROM DUAL;
SELECT CAST(XMLPARSE(DOCUMENT '<a>123</a>') AS CHAR(100)) FROM DUAL;
SELECT CAST(XMLPARSE(DOCUMENT '<a>123</a>') AS VARCHAR2(2)) FROM DUAL;
# Implicit conversion examples
SELECT UPPER(XMLPARSE(DOCUMENT'<a>123</a>')) FROM DUAL;
SELECT LOWER(XMLPARSE(DOCUMENT'<a>123</a>')) FROM DUAL;
SELECT SUBSTR(XMLPARSE(DOCUMENT'<a>123</a>')) FROM DUAL;
SELECT LENGTH(XMLPARSE(DOCUMENT'<a>123</a>')) FROM DUAL;
References
For more information about the conversion functions of the XMLType data type, see the following topics:
