日期时间数据类型

Snowflake 中的数据类型与函数

Jake Roach

Field Data Engineer

DATE 数据类型

  • 日期值用于存储事件发生的那一天
                            2023-04-01
                            YYYY-MM-DD
  CREATE TABLE sales (
      transaction_date DATE  -- Define a column of type `DATE`
  );
Snowflake 中的数据类型与函数

DATE 数据类型

SELECT

TO_DATE('2023-04-01') AS the_date, -- Convert a string into a DATE
'2023-04-01'::DATE AS casted_date -- Casted using <value>::DATE
;
                         the_date  | casted_date  
                       ----------- | ------------  
                        2023-04-01 | 2023-04-01
Snowflake 中的数据类型与函数

Time 数据类型

  • 记录事件发生的时、分、秒
                               08:24:08
                               HH:MM:SS
CREATE TABLE sales (
    transaction_time TIME  -- Create a column with the `TIME` keyword
);
  • 定义一个 TIME
Snowflake 中的数据类型与函数

Time 数据类型

SELECT

TO_TIME('08:24:04') AS the_time,
'08:24:04'::TIME AS casted_time
;
                     |  the_time  | casted_time |
                     | ---------- | ----------- |
                     |  08:24:08  |  08:24:08   |
  • TO_TIME 函数
  • 使用 :: 强制转换
Snowflake 中的数据类型与函数

时间戳数据类型

  • TIMESTAMP 同时包含日期与时间
                        2023-04-01 08:24:04
                        YYYY-MM-DD HH:MM:SS
CREATE TABLE (
    transaction_timestamp TIMESTAMP
);
Snowflake 中的数据类型与函数

时间戳数据类型

SELECT 
    TO_TIMESTAMP('2023-04-01 08:24:04') AS the_timestamp,
    '2023-04-01 08:24:04'::TIMESTAMP AS casted_timestamp          
;
  • 使用 TO_TIMESTAMP<value>::TIMESTAMP 将字符串转换为时间戳
  • 存储日期时间数据的最常见方式
                  the_timestamp     |    casted_timestamp     
              --------------------- | ----------------------  
               2024-04-01 08:24:08  |  2024-04-01 08:24:08    
Snowflake 中的数据类型与函数

示例

SELECT
    TO_DATE('2021-05-14') AS the_date,
    TO_TIME('06:13:00') AS the_time,
    TO_TIMESTAMP('2021-05-14T06:13:00') AS the_timestamp,


-- Extract the DATE from a TIMESTAMP, we could do the same with TIME! transaction_timestamp::DATE AS casted_date
;
           the_date   |   the_time  |     the_timestamp    | casted_date  
         ------------ | ----------- | -------------------- | ------------  
          2021-05-14  |   06:13:00  | 2021-05-14T06:13:00  |  2021-05-14
Snowflake 中的数据类型与函数

时区

默认情况下,DATETIMETIMESTAMP 存储时不含时区

$$

TIMESTAMP_NTZ

  • TIMESTAMP 的默认实现
  • 不存储时区

$$

TIMESTAMP_LTZ

  • 依赖本地时区

$$

TIMESTAMP_TZ

  • 用户可指定时区
1 https://docs.snowflake.com/en/sql-reference/data-types-datetime#timestamp-ltz-timestamp-ntz-timestamp-tz
Snowflake 中的数据类型与函数

Passons à la pratique !

Snowflake 中的数据类型与函数

Preparing Video For Download...