Snowflake snippets

How to create and use a SQL table function in Snowflake

Let's say you have the following table GLOBALSUPERSTORE_ORDERS

SELECT *
FROM GLOBAL_SUPERSTORE.PUBLIC.GLOBALSUPERSTORE_ORDERS
LIMIT 10;
Snowflake snippet 4

And you want to create a function to extract a table with only one-year orders, using a table function.

CREATE OR REPLACE FUNCTION GetOrdersOfYear(order_year number)
RETURNS TABLE (row_ID NUMBER, order_id VARCHAR)
AS
$$
    SELECT row_ID, order_id
    FROM GLOBALSUPERSTORE_ORDERS 
    WHERE ExtractYear(order_id) = order_year
$$
;
Snowflake snippet 5

Then you can use the function in a select, as the following example.

SELECT * 
FROM TABLE(GetOrdersOfYear(2013));
Snowflake snippet 6

Back to Snowflake cookbook page