Funzioni di data e ora

Introduzione a Redshift

Jason Myers

Principal Architect

Ottenere data e ora correnti

  • SYSDATE data e ora all'inizio della transazione
-- Ottieni data e ora correnti
SELECT SYSDATE;
timestamp
============================
2024-01-27 20:05:55.976353
  • GETDATE() data e ora all'inizio dell'istruzione, richiede le parentesi
-- Ottieni data e ora correnti
SELECT GETDATE();
timestamp
============================
2024-01-27 20:06:55.976353
Introduzione a Redshift

Comportamento delle funzioni di data e ora

Attenzione alle funzioni solo del leader node!

  • DATEDIFF invece di AGE
  • GETDATE/SYSDATE invece di funzioni specifiche del leader:
    • CURRENT_TIME
    • CURRENT_TIMESTAMP
    • ISFINITE
    • LOCALTIME
    • LOCALTIMESTAMP
    • NOW
Introduzione a Redshift

Troncare date e orari

  • TRUNC restituisce una data da un timestamp
-- Ottieni la data corrente in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT TRUNC(SYSDATE);
2024-01-27
  • DATE_TRUNC('datepart', timestamp) tronca a un datepart come ora o giorno
-- Trunca a minuto in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT DATE_TRUNC('minute', SYSDATE);
2024-01-27 20:05:55
1 https://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
Introduzione a Redshift

Ottenere parti di date e timestamp

  • DATE_PART(datepart, date or timestamp)
    • estrae la parte richiesta da una data o da un timestamp
-- Ottieni il mese corrente in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT DATE_PART(month, SYSDATE);
1
  • Può restituire più di month, day, year
    • Esempi: dayofweek, quarter, timezone
-- Ottieni il giorno della settimana corrente in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT DATE_PART(dayofweek, SYSDATE);
6
Introduzione a Redshift

Confrontare date e orari

  • DATE_CMP(date_1, date_2) confronto relativo

    • Restituisce -1 se date_1 è precedente
    • Restituisce 0 se le date sono uguali
    • Restituisce 1 se date_1 è successiva
  • Funzioni specifiche per tipo

    • DATE_CMP_TIMESTAMP
    • DATE_CMP_TIMESTAMPTZ
    • TIMESTAMP_CMP
    • TIMESTAMP_CMP_TIMESTAMPTZ
    • TIMESTAMPTZ_CMP
-- Confronta 5 date da una tabella in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
  SELECT date_col, 
         TRUNC(SYSDATE) AS current_date,
         DATE_CMP(date_col, SYSDATE)
    FROM combined_history_projections
ORDER BY date_col
   LIMIT 3;
 date_col  |  current_date | date_cmp
===========|===============|==========
2024-01-26 | 2024-01-27    |       -1
2024-01-27 | 2024-01-27    |        0
2024-01-28 | 2024-01-27    |        1
Introduzione a Redshift

Calcolare differenze

  • DATEDIFF(datepart, value_1, value_2)
  • Supporta date, time, timetz o timestamp in entrambe le posizioni
    • Deve contenere il datepart
  • Restituisce
    • un valore negativo se value_2 è precedente
    • 0 se le date sono uguali
    • un valore positivo se value_2 è successivo
Introduzione a Redshift

Usare DATEDIFF

-- Giorni fino a fine primo trimestre in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT DATEDIFF(day,TRUNC(SYSDATE),'2024-03-31') AS days_diff;
days_diff
===========
64
Introduzione a Redshift

Incrementare date e orari

  • DATEADD(datepart, quantity, value)
  • Supporta date, time, timetz o timestamp
  • quantity può essere negativa per sottrarre
-- Aggiungi una settimana a una data in base a 
-- SYSDATE del 2024-01-27 20:05:55.976353
SELECT TRUNC(SYSDATE) AS todays_date,
       TRUNC(DATEADD(week, 1, SYSDATE)) AS next_weeks_date;
todays_date | next_weeks_date
============|==================
2024-01-27  | 2024-02-03
Introduzione a Redshift

Incrementare date e orari... attenzione

  • Gli anni bisestili per mese restituiscono fine mese
-- Aggiungi anni per mesi a una data
SELECT DATEADD(month, 12, '2024-02-29');
2025-02-28 00:00:00
  • Gli anni bisestili per anno restituiscono il giorno successivo
-- Aggiungi anni per anno a una data
SELECT DATEADD(year, 1, '2024-02-29');
2025-03-01 00:00:00
Introduzione a Redshift

Passiamo alla pratica!

Introduzione a Redshift

Preparing Video For Download...