skip to Main Content

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;

2

Answers


  1. This process should work:

    1. Get the day of the week of your input date
    2. Subtract this from the day of the week for a Saturday (controlled by parameters e.g. WEEK_START)
    3. Add this value as a number of days to your input date
    Login or Signup to reply.
  2. 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;
    $$;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search