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/get below:

Website Navigation


GET | Snowflake Documentation

Categories:

Semi-structured and structured data functions (Extraction)

GET

Extracts a value from an ARRAY or an OBJECT (or a VARIANT that contains an ARRAY or OBJECT).

The function returns NULL if either of the arguments is NULL.

Note that this function should not be confused with the GET DML command.

See also:

GET_IGNORE_CASE , GET_PATH , :

Syntax

ARRAY (or VARIANT containing an ARRAY)

GET( <array> , <index> )

GET( <variant> , <index> )

Copy

OBJECT (or VARIANT containing an OBJECT)

GET( <object> , <field_name> )

GET( <variant> , <field_name> )

Copy

MAP

Arguments
array

An expression that evaluates to an ARRAY.

index

An expression that evaluates to an INTEGER. This specifies the position of the element to retrieve from the ARRAY. The position is 0-based, not 1-based.

If the index points outside of the array boundaries, or if the indexed element does not exist (in a sparse array):

variant

An expression that evaluates to a VARIANT that contains either an ARRAY or an OBJECT.

object

An expression that evaluates to an OBJECT that contains key-value pairs.

field_name

An expression that evaluates to a VARCHAR. This specifies the key in a key-value pair for which you want to retrieve the value.

field_name must not be an empty string.

If object is a structured OBJECT, you must specify a constant for field_name.

If object does not contain the specified key:

map

An expression that evaluates to a MAP.

key

The key in a key-value pair for which you want to retrieve the value.

If map does not contain the specified key, the function returns NULL.

Returns Usage notes Examples

Create a table with sample data:

CREATE TABLE vartab (a ARRAY, o OBJECT, v VARIANT);
INSERT INTO vartab (a, o, v) 
  SELECT
    ARRAY_CONSTRUCT(2.71, 3.14),
    OBJECT_CONSTRUCT('Ukraine', 'Kyiv'::VARIANT, 
                     'France',  'Paris'::VARIANT),
    TO_VARIANT(OBJECT_CONSTRUCT('weatherStationID', 42::VARIANT,
                     'timestamp', '2022-03-07 14:00'::TIMESTAMP_LTZ::VARIANT,
                     'temperature', 31.5::VARIANT,
                     'sensorType', 'indoor'::VARIANT))
    ;

Copy

SELECT a, o, v FROM vartab;
+---------+----------------------+-------------------------------------------------+
| A       | O                    | V                                               |
|---------+----------------------+-------------------------------------------------|
| [       | {                    | {                                               |
|   2.71, |   "France": "Paris", |   "sensorType": "indoor",                       |
|   3.14  |   "Ukraine": "Kyiv"  |   "temperature": 31.5,                          |
| ]       | }                    |   "timestamp": "2022-03-07 14:00:00.000 -0800", |
|         |                      |   "weatherStationID": 42                        |
|         |                      | }                                               |
+---------+----------------------+-------------------------------------------------+

Copy

Extract the first element of an ARRAY:

SELECT GET(a, 0) FROM vartab;
+-----------+
| GET(A, 0) |
|-----------|
| 2.71      |
+-----------+

Copy

Given the name of a country, extract the name of the capital city of that country from an OBJECT containing country names and capital cities:

SELECT GET(o, 'Ukraine') FROM vartab;
+-------------------+
| GET(O, 'UKRAINE') |
|-------------------|
| "Kyiv"            |
+-------------------+

Copy

Extract the temperature from a VARIANT that contains an OBJECT:

SELECT GET(v, 'temperature') FROM vartab;
+-----------------------+
| GET(V, 'TEMPERATURE') |
|-----------------------|
| 31.5                  |
+-----------------------+

Copy

For more detailed examples, see Querying Semi-structured Data.

For examples of using GET with XMLGET, see the Examples and Usage Notes sections in XMLGET.


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