XMLCAST specification
The XMLCAST specification returns the first operand (the cast operand) converted to the type specified by data-type.
XMLCAST supports casts involving XML values, including conversions between non-XML data types and the XML data type. Either the type of the cast operand or the specified data type must be XML. If both the type of the cast operand and the target data type are XML, XMLCAST acts as a no-op.
- expression
- If the cast operand is an expression, the result is the argument value converted to the specified target data type. The expression or the target data type must be the XML data type. expression cannot be a host variable or parameter marker.
- NULL
- If the cast operand is the NULL keyword, the target data type must be the XML data type. The result is a null XML value.
- parameter-marker
- If the cast operand is a parameter marker, the target data type must be the XML data type. A parameter marker (specified as a question mark character) is normally considered to be an expression, but in this case because it has special meaning. When the cast operand is a parameter-marker, the data type that is specified represents the "promise" that the replacement value for the parameter marker will be assignable to the specified data type (using assignment rules). Such a parameter marker is considered to be a typed parameter marker, which is treated like any other typed value for the purpose of function resolution, a describe operation on a select list, or column assignment.
- data-type
- The name of an SQL data type. If the name is not qualified, the
SQL path is used to perform data type resolution. data-type must
not specify a distinct type. If a data type has associated attributes,
such as length or precision and scale, these attributes should be
included when specifying a value for data-type.
CHAR defaults to a length of 1, and DECIMAL defaults to a precision
of 5 and a scale of 0 if not specified. CLOB and DBCLOB default to
a length of 1M. When
the target data type is XML and the source data type is TIMESTAMP,
trailing zeroes in the fractional seconds part of the value are not
included in the result. Restrictions on the supported data types
are based on the specified cast operand. The default encoding scheme
for string data types is Unicode. The encoding scheme can be changed
by specifying the CCSID clause.
Table 1. Supported conversions from Non-XML values to XML values Source data type Target data type: XML Resulting XML schema type DATE Y xs:date TIME Y xs:time TIMESTAMP(p) WITH TIME ZONE Y xs:dateTime
Examples
Example 1: Create a null
XML value.
XMLCAST(NULL AS XML)
Example
2: Convert a value extracted from an XMLQUERY expression into
an INTEGER:
XMLCAST(XMLQUERY('/PRODUCT/QUANTITY'
PASSING xmlcol) AS INTEGER)
Example 3: Convert
a value extracted from an XMLQUERY expression into a varying-length
character string:
XMLCAST(XMLQUERY('/PRODUCT/NAME'
PASSING xmlcol) AS VARCHAR(20))
Note that in the above two examples, if the XMLQUERY returns a sequence of more than one node, the XMLCAST specification will return an error.
Example
4: Convert a value extracted from an SQL scalar subquery into
an XML value:
XMLCAST((SELECT quantity FROM product AS p
WHERE p.id = 1077) AS XML)