In OceanBase Database, the XMLType data type can be implicitly converted to the VARCHAR data type.
Note that it is not recommended to use XMLType as a string. OceanBase Database stores XMLType data in binary format, which is optimized for XPath queries and removes the original formatting information provided by users, such as line breaks, indentation, and whitespace in XML element that are not required by the XML specification. If you want to adjust the output format of XMLType data, you can use the XMLSERIALIZE() function.
The following table describes the conversion rules between the XMLType data type and other data types.
| 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 data type. |
| XMLType | BLOB | Not supported. | Not supported. However, you can use the XMLSERIALIZE() function to convert XMLType data to the BLOB data type. |
| XMLType | Other primitive data types | Not supported. | Not supported. However, you can use the XMLCAST() function to convert text in an XML document to other primitive data types. |
| VARCHAR2 | XMLType | VARCHAR2 data can be directly inserted into an XMLType column, but it cannot be directly used as a parameter in functions that take XMLType as input, such as EXTRACTVALUE(). |
Not supported. |
| CLOB | XMLType | Not supported. | Not supported. |
| BLOB | XMLType | Not supported. | Not supported. |
| Other primitive data 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 using the CAST function. However, these data types cannot be converted to the XMLType data type 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 functions related to the conversion of the XMLType data type, see the following topics: