TO_XML
functionsupportedCore SQL functionsConverts a VARIANT to a VARCHAR that contains an XML representation of the value. If the input is NULL, the result is also NULL.
Syntax
TO_XML( <expression> )
Parameters
| Name | Required | Description |
|---|---|---|
expression | required | An expression that evaluates to a VARIANT or that can be cast to a VARIANT. |
Returns
The data type of the returned value is VARCHAR.
Usage notes
- Common uses for this function include: Generating a string that contains an XML-formatted value that matches an originally inserted XML-formatted value.
- Converting a semi-structured value (which doesn’t necessarily need to have been formatted as XML originally) into an XML-formatted value. For example, you can use TO_XML to generate an XML-compatible representation of a value that was originally formatted as JSON.
- If the input expression doesn’t evaluate to a VARIANT, Snowflake implicitly casts the result of the expression to a VARIANT. Because all other Snowflake data types can be cast to VARIANT, this means that a value of any data type can be passed to TO_XML and converted to an XML-formatted string. (The GEOGRAPHY data type is a partial exception; to call TO_XML with a value of type GEOGRAPHY, you must explicitly cast the GEOGRAPHY value to VARIANT.)
- If the value didn’t originate as XML, then Snowflake generates XML-compatible tags. These tags may use the type attribute to specify the Snowflake data type of the tag’s contents. Below are examples of tags generated by Snowflake. The outermost tag pair is similar to the following: Copy code <SnowflakeData type="OBJECT"> </SnowflakeData> The data type specified in the type attribute of the tag can vary. For an OBJECT, each key-value pair’s tags are based on the key. For example: Copy code <key1 type="VARCHAR">value1</key1> For an ARRAY, each element of the array is in a tag pair similar to: Copy code <e type="VARCHAR"> </e> Here is a complete example of the XML for a simple OBJECT that contains two key-value pairs: Copy code <SnowflakeData type="OBJECT"> <key1 type="VARCHAR">value1</key1> <key2 type="VARCHAR">value2</key2> </SnowflakeData> Here is a complete example of the XML for a simple ARRAY that contains two VARCHAR values: Copy code <SnowflakeData type="ARRAY"> <e type="VARCHAR">v1</e> <e type="VARCHAR">v2</e> </SnowflakeData>