Datetime data types

Data Types and Functions in Snowflake

Jake Roach

Field Data Engineer

DATE data types

  • Date values store the day on which something occurred
                            2023-04-01
                            YYYY-MM-DD
  CREATE TABLE sales (
      transaction_date DATE  -- Define a column of type `DATE`
  );
Data Types and Functions in Snowflake

DATE data types

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
Data Types and Functions in Snowflake

Time data types

  • Captures the hours, minutes, and seconds that some event occurred
                               08:24:08
                               HH:MM:SS
CREATE TABLE sales (
    transaction_time TIME  -- Create a column with the `TIME` keyword
);
  • Define a TIME column
Data Types and Functions in Snowflake

Time data types

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 function
  • Cast using ::
Data Types and Functions in Snowflake

Timestamp data types

  • TIMESTAMP captures both date and time
                        2023-04-01 08:24:04
                        YYYY-MM-DD HH:MM:SS
CREATE TABLE (
    transaction_timestamp TIMESTAMP
);
Data Types and Functions in Snowflake

Timestamp data types

SELECT 
    TO_TIMESTAMP('2023-04-01 08:24:04') AS the_timestamp,
    '2023-04-01 08:24:04'::TIMESTAMP AS casted_timestamp          
;
  • Convert a string to a timestamp using TO_TIMESTAMP or <value>::TIMESTAMP
  • Most common way to store datetime data
                  the_timestamp     |    casted_timestamp     
              --------------------- | ----------------------  
               2024-04-01 08:24:08  |  2024-04-01 08:24:08    
Data Types and Functions in Snowflake

Examples

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
Data Types and Functions in Snowflake

Timezones

By default, DATE, TIME, and TIMESTAMP's are stored without a timezone

$$

TIMESTAMP_NTZ

  • Default implementation of TIMESTAMP
  • No timezone is stored

$$

TIMESTAMP_LTZ

  • Relies on local timezone

$$

TIMESTAMP_TZ

  • User specifies timezone
1 https://docs.snowflake.com/en/sql-reference/data-types-datetime#timestamp-ltz-timestamp-ntz-timestamp-tz
Data Types and Functions in Snowflake

Let's practice!

Data Types and Functions in Snowflake

Preparing Video For Download...