A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/functions/to_xml below:

Website Navigation


TO_XML | Snowflake Documentation

Categories:

Conversion functions , Semi-structured and structured data functions (Cast)

TO_XML

Converts a VARIANT to a VARCHAR that contains an XML representation of the value. If the input is NULL, the result is also NULL.

See also:

CHECK_XML, PARSE_XML, XMLGET

Syntax Arguments
expression

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 Examples

This example shows how to use the function if you’ve loaded XML-formatted data into an OBJECT by calling PARSE_XML.

Create a table and insert data:

CREATE OR REPLACE TABLE xml_02 (x OBJECT);

INSERT INTO xml_02 (x)
  SELECT PARSE_XML('<note> <body>Sample XML</body> </note>');

Copy

Call the TO_XML and TO_VARCHAR functions:

SELECT x, TO_VARCHAR(x), TO_XML(x) FROM xml_02;

Copy

+---------------------------+--------------------------------------+--------------------------------------+
| X                         | TO_VARCHAR(X)                        | TO_XML(X)                            |
|---------------------------+--------------------------------------+--------------------------------------|
| <note>                    | <note><body>Sample XML</body></note> | <note><body>Sample XML</body></note> |
|   <body>Sample XML</body> |                                      |                                      |
| </note>                   |                                      |                                      |
+---------------------------+--------------------------------------+--------------------------------------+

You can also call the TO_XML function with data that did not originate as XML-formatted data, as shown in the examples below.

The following example creates a simple OBJECT and then generates the corresponding XML. The XML output contains information about the data types of the values in the key-value pairs, as well as the data type of the overall value (OBJECT).

CREATE OR REPLACE TABLE xml_03 (object_col_1 OBJECT);

INSERT INTO xml_03 (object_col_1)
  SELECT OBJECT_CONSTRUCT('key1', 'value1', 'key2', 'value2');

Copy

SELECT object_col_1, TO_XML(object_col_1)
  FROM xml_03;

Copy

+---------------------+-------------------------------------------------------------------------------------------------------------------+
| OBJECT_COL_1        | TO_XML(OBJECT_COL_1)                                                                                              |
|---------------------+-------------------------------------------------------------------------------------------------------------------|
| {                   | <SnowflakeData type="OBJECT"><key1 type="VARCHAR">value1</key1><key2 type="VARCHAR">value2</key2></SnowflakeData> |
|   "key1": "value1", |                                                                                                                   |
|   "key2": "value2"  |                                                                                                                   |
| }                   |                                                                                                                   |
+---------------------+-------------------------------------------------------------------------------------------------------------------+

The following example creates a simple ARRAY and then generates the corresponding XML. The XML output contains information about the data types of the array elements, as well as the data type of the overall value (ARRAY).

CREATE OR REPLACE TABLE xml_04 (array_col_1 ARRAY);

INSERT INTO xml_04 (array_col_1)
  SELECT ARRAY_CONSTRUCT('v1', 'v2');

Copy

SELECT array_col_1, TO_XML(array_col_1)
  FROM xml_04;

Copy

+-------------+----------------------------------------------------------------------------------------------+
| ARRAY_COL_1 | TO_XML(ARRAY_COL_1)                                                                          |
|-------------+----------------------------------------------------------------------------------------------|
| [           | <SnowflakeData type="ARRAY"><e type="VARCHAR">v1</e><e type="VARCHAR">v2</e></SnowflakeData> |
|   "v1",     |                                                                                              |
|   "v2"      |                                                                                              |
| ]           |                                                                                              |
+-------------+----------------------------------------------------------------------------------------------+

The following example inserts data that is in JSON format and then generates the corresponding XML.

CREATE OR REPLACE TABLE xml_05 (json_col_1 VARIANT);

INSERT INTO xml_05 (json_col_1)
  SELECT PARSE_JSON(' { "key1": ["a1", "a2"] } ');

Copy

SELECT json_col_1,
       TO_JSON(json_col_1),
       TO_XML(json_col_1)
  FROM xml_05;

Copy

+-------------+----------------------+-------------------------------------------------------------------------------------------------------------------------+
| JSON_COL_1  | TO_JSON(JSON_COL_1)  | TO_XML(JSON_COL_1)                                                                                                      |
|-------------+----------------------+-------------------------------------------------------------------------------------------------------------------------|
| {           | {"key1":["a1","a2"]} | <SnowflakeData type="OBJECT"><key1 type="ARRAY"><e type="VARCHAR">a1</e><e type="VARCHAR">a2</e></key1></SnowflakeData> |
|   "key1": [ |                      |                                                                                                                         |
|     "a1",   |                      |                                                                                                                         |
|     "a2"    |                      |                                                                                                                         |
|   ]         |                      |                                                                                                                         |
| }           |                      |                                                                                                                         |
+-------------+----------------------+-------------------------------------------------------------------------------------------------------------------------+

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4