Create table in SNOWFLAKE With Time stamp in UTC

Viewed 1025

I m trying to create. stable in snowflake, making sure that time stamp is CURRENT_TIMESTAMP() but in UTC. Here is the syntax I am using;

 ALTER SESSION SET TIMEZONE = 'UTC';
CREATE OR REPLACE TABLE DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES_DATAROBOT(
 ACCOUNT_ID NUMBER NOT NULL PRIMARY KEY,
 PREDICTED_PROBABILITY FLOAT,
TIME_PREDICTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

But after inserting rows into this table, I am still getting the time in America/Los Angeles time format.

479291 0.5797053576  2021-06-16 13:56:26.595

Can I kindly get help on how to get timestamp in UTC? Help is appreciated.

2 Answers

There's a few options for timestamps and converting between timezones. I'd recommend familiarizing yourself with the three different kinds of timestamps

This is generally what we use though: CONVERT_TIMEZONE('UTC', current_timestamp())::timestamp_ntz

The convert should get you to UTC, regardless of what timezone your Snowflake instance is set to by default. And the timestamp_ntz (no time zone) is the timestamp without any timezone information, which works well if you know it's always going to be UTC.

Without knowing a lot more about your use case I'd have a hard time giving a wholehearted recommendation on which of the timezone data types you should be using. This code should only need a minor tweak to change the data type.

EDIT: More full test example

CREATE OR REPLACE TABLE DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES_DATAROBOT(
 ACCOUNT_ID NUMBER NOT NULL PRIMARY KEY,
TIME_PREDICTED1 TIMESTAMP_NTZ DEFAULT CONVERT_TIMEZONE('UTC',CURRENT_TIMESTAMP)::timestamp_ntz,
TIME_PREDICTED2 TIMESTAMP_NTZ DEFAULT CONVERT_TIMEZONE('America/Los_Angeles',CURRENT_TIMESTAMP)::timestamp_ntz
);

insert into DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES_DATAROBOT (account_id)
select 1;

select * from DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES_DATAROBOT;

Edit 2: I think the reason your version isn't working is because the Session timezone isn't "remembered" by the default constraint. That default will use whatever the session timezone is for the insert queries in the future, which is another reason that it's good to specify the timezone explicitely here.

David's answer works. you have to change the data type to TIMESTAMPNTZ too

CREATE OR REPLACE TABLE DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES_DATAROBO( ACCOUNT_ID NUMBER NOT NULL PRIMARY KEY, PREDICTED_PROBABILITY FLOAT, TIME_PREDICTED TIMESTAMPNTZ DEFAULT CONVERT_TIMEZONE('UTC', current_timestamp())::TIMESTAMPNTZ );

Related