Tipuri de date datetime

Tipuri de date și funcții în Snowflake

Jake Roach

Field Data Engineer

Tipuri de date DATE

  • Valorile de tip DATE stochează ziua în care a avut loc un eveniment
                            2023-04-01
                            YYYY-MM-DD
  CREATE TABLE sales (
      transaction_date DATE  -- Define a column of type `DATE`
  );
Tipuri de date și funcții în Snowflake

Tipuri de date 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
Tipuri de date și funcții în Snowflake

Tipuri de date TIME

  • Captează orele, minutele și secundele la care a avut loc un eveniment
                               08:24:08
                               HH:MM:SS
CREATE TABLE sales (
    transaction_time TIME  -- Create a column with the `TIME` keyword
);
  • Definirea unei coloane TIME
Tipuri de date și funcții în Snowflake

Tipuri de date 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   |
  • Funcția TO_TIME
  • Conversie cu ::
Tipuri de date și funcții în Snowflake

Tipuri de date timestamp

  • TIMESTAMP captează atât data, cât și ora
                        2023-04-01 08:24:04
                        YYYY-MM-DD HH:MM:SS
CREATE TABLE (
    transaction_timestamp TIMESTAMP
);
Tipuri de date și funcții în Snowflake

Tipuri de date timestamp

SELECT 
    TO_TIMESTAMP('2023-04-01 08:24:04') AS the_timestamp,
    '2023-04-01 08:24:04'::TIMESTAMP AS casted_timestamp          
;
  • Convertiți un șir în timestamp folosind TO_TIMESTAMP sau <value>::TIMESTAMP
  • Cel mai comun mod de a stoca date datetime
                  the_timestamp     |    casted_timestamp     
              --------------------- | ----------------------  
               2024-04-01 08:24:08  |  2024-04-01 08:24:08    
Tipuri de date și funcții în Snowflake

Exemple

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
Tipuri de date și funcții în Snowflake

Fusuri orare

Implicit, DATE, TIME și TIMESTAMP sunt stocate fără fus orar

$$

TIMESTAMP_NTZ

  • Implementarea implicită a TIMESTAMP
  • Nu se stochează niciun fus orar

$$

TIMESTAMP_LTZ

  • Utilizează fusul orar local

$$

TIMESTAMP_TZ

  • Utilizatorul specifică fusul orar
1 https://docs.snowflake.com/en/sql-reference/data-types-datetime#timestamp-ltz-timestamp-ntz-timestamp-tz
Tipuri de date și funcții în Snowflake

Să exersăm!

Tipuri de date și funcții în Snowflake

Preparing Video For Download...