0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Snowflake Timestamp型の比較

Posted at

Snowflakeで使えるTimestamp型の比較

Snowflake Timestamp型 PostgreSQL Timestamp型 保存時のデータ 参照時の出力
TIMESTAMP_LTZ TIMESTAMP WITH LOCAL TIME ZONE UTC 操作時のタイムゾーンを参照
TIMESTAMP_NTZ TIMESTAMP WITHOUT TIME ZONE タイムゾーンを無視 タイムゾーンを無視
TIMESTAMP_TZ TIMESTAMP WITH TIME ZONE タイムゾーンオフセット付き 保存されたタイムゾーンを参照

SnowflakeのTimestamp型から動作をイメージできないので、エイリアスにもなっているPostgreSQLのTimestamp型を追記。

確認用SQLと出力結果

動作確認をする。

確認用SQL

create or replace table ts(
    "挿入データ" string, 
    "TIMESTAMP_LTZ" timestamp_ltz, 
    "TIMESTAMP_NTZ" timestamp_ntz, 
    "TIMESTAMP_TZ" timestamp_tz
);

alter session set timezone = 'America/Adak'; -- ハワイ - アリューシャン標準時 -10
insert into ts values
    (
        '2014-01-01 00:00:00',
        '2014-01-01 00:00:00',
        '2014-01-01 00:00:00',
        '2014-01-01 00:00:00'
    ),(
        '2014-01-01 00:00:00 +00:00',
        '2014-01-01 00:00:00 +00:00',
        '2014-01-01 00:00:00 +00:00',
        '2014-01-01 00:00:00 +00:00'
    ),(
        '2014-01-01 00:00:00 +09:00',
        '2014-01-01 00:00:00 +09:00',
        '2014-01-01 00:00:00 +09:00',
        '2014-01-01 00:00:00 +09:00'
    )
;

alter session set timezone = 'Asia/Kolkata'; -- インド標準時 +5.5
-- alter session set timezone = 'UTC';
select * from ts;

出力結果

挿入データ TIMESTAMP_LTZ TIMESTAMP_NTZ TIMESTAMP_TZ
2014-01-01 00:00:00 2014-01-01 15:30:00.000 +0530 2014-01-01 00:00:00.000 2014-01-01 00:00:00.000 -1000
2014-01-01 00:00:00 +00:00 2014-01-01 05:30:00.000 +0530 2014-01-01 00:00:00.000 2014-01-01 00:00:00.000 +0000
2014-01-01 00:00:00 +09:00 2013-12-31 20:30:00.000 +0530 2014-01-01 00:00:00.000 2014-01-01 00:00:00.000 +0900

TIMESTAMP_LTZ型を使った時の変換が脳内計算できないので、それぞれの操作時のタイムゾーンと、タイムゾーンがUTCの場合の列を追記。

挿入データ(America/Adak -10) TIMESTAMP_LTZ (UTC) TIMESTAMP_LTZ (Asia/Kolkata +5.5) TIMESTAMP_NTZ (Asia/Kolkata +5.5) TIMESTAMP_TZ (Asia/Kolkata +5.5)
2014-01-01 0:00:00 2014-01-01 10:00:00.000 +0000 2014-01-01 15:30:00.000 +0530 2014-01-01 0:00:00 2014-01-01 00:00:00.000 -1000
2014-01-01 00:00:00 +00:00 2014-01-01 00:00:00.000 +0000 2014-01-01 05:30:00.000 +0530 2014-01-01 0:00:00 2014-01-01 00:00:00.000 +0000
2014-01-01 00:00:00 +09:00 2013-12-31 15:00:00.000 +0000 2013-12-31 20:30:00.000 +0530 2014-01-01 0:00:00 2014-01-01 00:00:00.000 +0900

オフセット無しでデータを挿入すると、セッションのTIMEZONEが自動的に付与される。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?