Search code examples
databasedb2clob

Converting DBCLOB/CLOB to XML


We are facing a problem in converting/casting DBCLOB to XML.

Background

We are storing some xml data in a column of type DBCLOB (1073741823). For one of our requirements, we have to convert this data to XML type so that we can take advantage of Xquery to filter the result. For doing this conversion, we are using the following SQL query to convert DBCLOB to XML data type.

SELECT XMLCAST (XMLPARSE (DOCUMENT (CAST (CAST (COLUMN1 AS DBCLOB(32672)) AS VARCHAR (32672)))) AS XML from TABLE1 

Problem

For some scenario the size of data in DBCLOB column is more than 32672 and, since we are converting DBCLOB to XML via VARCHAR, so the output get limited to 32672, and XML conversion fails.

What would be the way to achieve this casting (clob to xml)

Thanks in advance


Solution

  • Actually i was casting it to varchar as XMLPARSE function was expecting a string expression.

    After going through the documentation again, i converted it to blob and then to XML. It worked, the sample query which worked is given below for reference.

    SELECT 
      XMLCAST (
        XMLPARSE (
          DOCUMENT CAST (
            COLUMN1 AS BLOB
          ) 
          PRESERVE WHITESPACE
        ) as XML
      ) 
    FROM 
      TABLE1
    

    Thanks for support