Date and time functions

Introduction to Redshift

Jason Myers

Principal Architect

Getting current date and time

  • SYSDATE date and time at transaction start
-- Get current date and time
SELECT SYSDATE;
timestamp
============================
2024-01-27 20:05:55.976353
  • GETDATE() date and time at statement start, requires parenthesis
-- Get current date and time
SELECT GETDATE();
timestamp
============================
2024-01-27 20:06:55.976353
Introduction to Redshift

Date and time function behavior

Watch out for leader node-only functions!

  • DATEDIFF over AGE
  • GETDATE/SYSDATE over leader-specific functions:
    • CURRENT_TIME
    • CURRENT_TIMESTAMP
    • ISFINITE
    • LOCALTIME
    • LOCALTIMESTAMP
    • NOW
Introduction to Redshift

Truncating dates and times

  • TRUNC returns a date from a timestamp
-- Get current date based on 
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT TRUNC(SYSDATE);
2024-01-27
  • DATE_TRUNC('datepart', timestamp) truncates to a datepart like hour or day
-- Truncate to hour based on 
-- SYSDATE of 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
Introduction to Redshift

Getting parts of dates and timestamps

  • DATE_PART(datepart, date or timestamp)
    • extracts the requested part from a date or timestamp
-- Get current month based on 
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_PART(month, SYSDATE);
1
  • Can return more than month, day, year
    • Examples: dayofweek, quarter, timezone
-- Get current day of week based on 
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_PART(dayofweek, SYSDATE);
6
Introduction to Redshift

Comparing dates and times

  • DATE_CMP(date_1, date_2) relative comparison

    • Returns -1 if date_1 is earlier
    • Returns 0 if dates are equal
    • Returns 1 if date_1 is later
  • Type specific functions

    • DATE_CMP_TIMESTAMP
    • DATE_CMP_TIMESTAMPTZ
    • TIMESTAMP_CMP
    • TIMESTAMP_CMP_TIMESTAMPTZ
    • TIMESTAMPTZ_CMP
-- Compare 5 dates from a table based on 
-- SYSDATE of 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
Introduction to Redshift

Calculating differences

  • DATEDIFF(datepart, value_1, value_2)
  • Supports date, time, timetz, or timestamp in either position
    • Must contain the datepart
  • Returns
    • a negative value if value_2 is earlier
    • Returns 0 if dates are equal
    • a positive value if value_2 is later
Introduction to Redshift

Using DATEDIFF

-- Days till end of first quarter based on 
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATEDIFF(day,TRUNC(SYSDATE),'2024-03-31') AS days_diff;
days_diff
===========
64
Introduction to Redshift

Incrementing dates and times

  • DATEADD(datepart, quantity, value)
  • Supports date, time, timetz, or timestamp
  • Quantity can be negative to subtract
-- Add week to a date based on 
-- SYSDATE of 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
Introduction to Redshift

Incrementing dates and times... gotchas

  • Leap years by month return end of month
-- Add year by months to a date
SELECT DATEADD(month, 12, '2024-02-29');
2025-02-28 00:00:00
  • Leap years by year return next day
-- Add year by year to a date
SELECT DATEADD(year, 1, '2024-02-29');
2025-03-01 00:00:00
Introduction to Redshift

Let's practice!

Introduction to Redshift

Preparing Video For Download...