Categories:

Semi-structured and Structured Data Functions (Cast)

AS_ARRAY¶

Casts a VARIANT value to an array.

See also:

AS_<object_type> , AS_OBJECT

Syntax¶

AS_ARRAY( <variant_expr> )
Copy

Arguments¶

variant_expr

An expression that evaluates to a value of type VARIANT.

Usage Notes¶

  • If the variant_expr does not contain a value of type ARRAY, then the function returns NULL.

Examples¶

This shows how to use the function:

Create a table and data:

CREATE TABLE multiple_types (
    array1 VARIANT,
    array2 VARIANT,
    boolean1 VARIANT,
    char1 VARIANT,
    varchar1 VARIANT,
    decimal1 VARIANT,
    double1 VARIANT,
    integer1 VARIANT,
    object1 VARIANT
    );
INSERT INTO multiple_types 
     (array1, array2, boolean1, char1, varchar1, 
      decimal1, double1, integer1, object1)
   SELECT 
     TO_VARIANT(TO_ARRAY('Example')), 
     TO_VARIANT(ARRAY_CONSTRUCT('Array-like', 'example')), 
     TO_VARIANT(TRUE), 
     TO_VARIANT('X'), 
     TO_VARIANT('I am a real character'), 
     TO_VARIANT(1.23::DECIMAL(6, 3)),
     TO_VARIANT(3.21::DOUBLE),
     TO_VARIANT(15),
     TO_VARIANT(TO_OBJECT(PARSE_JSON('{"Tree": "Pine"}')))
     ;
Copy

Now run the query. The true arrays that were stored as VARIANT values are converted back to ARRAY values. However, the value in column object1, which wasn’t a true ARRAY, is converted to NULL.

SELECT 
       AS_ARRAY(array1) AS "ARRAY1",
       AS_ARRAY(array2) AS "ARRAY2",
       AS_OBJECT(object1) AS "OBJECT",
       AS_ARRAY(object1) AS "OBJECT AS ARRAY"
  FROM multiple_types;
+-------------+-----------------+------------------+-----------------+
| ARRAY1      | ARRAY2          | OBJECT           | OBJECT AS ARRAY |
|-------------+-----------------+------------------+-----------------|
| [           | [               | {                | NULL            |
|   "Example" |   "Array-like", |   "Tree": "Pine" |                 |
| ]           |   "example"     | }                |                 |
|             | ]               |                  |                 |
+-------------+-----------------+------------------+-----------------+
Copy