Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda