Introduction to Redshift
Jason Myers
Principal Architect
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
Watch out for leader node-only functions!
DATEDIFF
over AGE
GETDATE
/SYSDATE
over leader-specific functions:CURRENT_TIME
CURRENT_TIMESTAMP
ISFINITE
LOCALTIME
LOCALTIMESTAMP
NOW
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
DATE_PART(datepart, date or timestamp)
-- Get current month based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_PART(month, SYSDATE);
1
month
, day
, year
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
DATE_CMP(date_1, date_2)
relative comparison
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
DATEDIFF(datepart, value_1, value_2)
-- 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
DATEADD(datepart, quantity, value)
-- 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
-- Add year by months to a date
SELECT DATEADD(month, 12, '2024-02-29');
2025-02-28 00:00:00
-- Add year by year to a date
SELECT DATEADD(year, 1, '2024-02-29');
2025-03-01 00:00:00
Introduction to Redshift