ชนิดข้อมูลวันที่และเวลา

ชนิดข้อมูลและฟังก์ชันใน Snowflake

Jake Roach

Field Data Engineer

ชนิดข้อมูล DATE

  • ค่า 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

  • TIMESTAMP เก็บทั้งวันที่และเวลา
                        2023-04-01 08:24:04
                        YYYY-MM-DD HH:MM:SS
CREATE TABLE (
    transaction_timestamp TIMESTAMP
);
ชนิดข้อมูลและฟังก์ชันใน Snowflake

ชนิดข้อมูล Timestamp

SELECT 
    TO_TIMESTAMP('2023-04-01 08:24:04') AS the_timestamp,
    '2023-04-01 08:24:04'::TIMESTAMP AS casted_timestamp          
;
  • แปลงสตริงเป็น 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

Timezone

โดยค่าเริ่มต้น DATE, TIME และ TIMESTAMP จะถูกเก็บโดยไม่มีข้อมูล timezone

$$

TIMESTAMP_NTZ

  • การใช้งาน TIMESTAMP แบบค่าเริ่มต้น
  • ไม่มีการเก็บ timezone

$$

TIMESTAMP_LTZ

  • ใช้ timezone ของเครื่อง (local)

$$

TIMESTAMP_TZ

  • ผู้ใช้ระบุ timezone เอง
1 https://docs.snowflake.com/en/sql-reference/data-types-datetime#timestamp-ltz-timestamp-ntz-timestamp-tz
ชนิดข้อมูลและฟังก์ชันใน Snowflake

มาฝึกกันเถอะ!

ชนิดข้อมูลและฟังก์ชันใน Snowflake

Preparing Video For Download...