javascriptazurestored-proceduressnowflake-cloud-data-platformsnowflake-stage

Snowflake : Get next Saturday date from today using stored procedure


I need to get the upcoming saturday based on the date field in table. I need to implement code using snowflake javascript stored procedure. Suggestions appreciated.

When following query is executed using snowflake console, I get result.

SELECT next_day(TO_DATE(MAX(SALE_DATE)),'Saturday') FROM Sales;

But when I implement the same using stored procedure it throws error "unexpected Saturday"

Tried to fetch the upcoming Saturday date based on the maximum date in table

  var get_cm = "SELECT next_day(TO_DATE(MAX(SALE_DATE)),'Saturday') FROM Sales";
   var get_sql= snowflake.createStatement({sqlText:get_cm);
   var res_dt = get_sql.execute();
   res_dt.next();
   var res = res_dt.getColumnValue(1);
   return res;

Solution

  • I adjusted your JS code slightly and it worked for me:

    CREATE OR REPLACE procedure NEXT_SATURDAY_TEST()
    RETURNS STRING
    LANGUAGE JAVASCRIPT
    AS
    $$
       var get_cm = "SELECT next_day(TO_DATE(MAX(SALE_DATE)),'Saturday') FROM Sales;";
       var res_dt = snowflake.execute({sqlText: get_cm});
       res_dt.next();
       var res = res_dt.getColumnValue(1);
       return res;
    $$;