Estoy consultando una tabla y almacenando la fecha y luego necesito insertar esa fecha como una marca de tiempo en otra tabla.
Actualmente, estoy usando querieddate.toISOString() pero eso manipula la fecha y establece 5 horas por delante.
¿Cómo puedo evitar eso?
¿No puedes simplemente usar TO_TIMESTAMP_NTZ en la FECHA?
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;| FECHA_A | FECHA_A_TIPO | TIMESTAMP_A | TIMESTAMP_A_TYPE |
|---|---|---|---|
| 2022-03-22 | FECHA[SB4] | 2022-03-22 00:00:00.000 | MARCA DE TIEMPO_NTZ(9)[SB16] |
Entonces, sí, dentro de JavaScript, está expuesto al hecho de que la zona horaria predeterminada en Snowflake no es UTC, lo que creo que es algo pobre. Pero ahí lo tienes:
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 |
PERO si también obtenemos de date_table la fecha como epoch segundos como una cadena (gemido)... podemos insertar eso en una marca de tiempo y evitar la incapacidad de Javascript para contener grandes números.
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 |
En su lugar, puede mantener la fecha como una variable de sesión SQL como esta
snowflake.execute({sqlText:`set start_date = current_timestamp();`); snowflake.execute({sqlText:`select * from table where date > $start_date`);