Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Snowflake stored procedure timezone without manipulation

I am querying a table and storing the date and then need to insert that date as a timestamp to another table.

Currently, I'm using querieddate.toISOString() but that manipulates the date and sets 5 hours ahead.

How can I avoid that?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Can you not just use TO_TIMESTAMP_NTZ on the DATE?

SELECT to_date('2022-03-22') as date_a
    ,SYSTEM$TYPEOF(date_a) as date_a_type
    ,to_timestamp_ntz(date_a) as timestamp_a
    ,SYSTEM$TYPEOF(timestamp_a) as timestamp_a_type;
DATE_A DATE_A_TYPE TIMESTAMP_A TIMESTAMP_A_TYPE
2022-03-22 DATE[SB4] 2022-03-22 00:00:00.000 TIMESTAMP_NTZ(9)[SB16]

So yes, within the JavaScript, you are being exposed to the fact the default timezone in Snowflake is not UTC, which I think is somewhat poor. But there you have it:

CREATE TABLE date_table(date date);
INSERT INTO date_table values (to_date('2022-03-22'));

CREATE TABLE timestamp_table(ts timestamp);

create or replace procedure stproc1()
  returns string not null
  language javascript
  as
  -- "$$" is the delimiter for the beginning and end of the stored procedure.
  $$
    var sql_command = "SELECT TO_TIMESTAMP(date) FROM date_table limit 1;";
    var stmt = snowflake.createStatement( {sqlText: sql_command} );
    var resultSet = stmt.execute();
    resultSet.next();
    var my_sfDate = resultSet.getColumnValue(1);
    
    return my_sfDate.toISOString();
  $$
  ;

call stproc1();
STPROC1
2022-03-22T07:00:00.000Z

BUT if we also fetch from the date_table the date as epoch seconds as a string (groan).. we can poke that into a timestamp, and avoid Javascripts inability to hold large numbers.

create or replace procedure stproc1()
  returns string not null
  language javascript
  as
  -- "$$" is the delimiter for the beginning and end of the stored procedure.
  $$
    var sql_command = "SELECT date, extract('epoch_seconds', date)::text as epoch_secs FROM date_table limit 1;";
    var stmt = snowflake.createStatement( {sqlText: sql_command} );
    var resultSet = stmt.execute();
    resultSet.next();
    var my_sfDate = resultSet.getColumnValue(1);
    var my_epochSec = resultSet.getColumnValue(2);
    
    //return my_epochSec;
     
    sql_command2 = "INSERT INTO timestamp_table values(:1)";
    stmt2 = snowflake.createStatement( {sqlText: sql_command2, binds:[my_epochSec]} );
    var resultSet = stmt2.execute();
    return 0;
  $$
  ;
call stproc1();
select * from timestamp_table;
TS
2022-03-22 00:00:00.000
about 4 years ago · Juan Pablo Isaza Report

0

You can instead keep the date as a SQL session variable like this

snowflake.execute({sqlText:`set start_date = current_timestamp();`);

snowflake.execute({sqlText:`select * from table where date > $start_date`);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!