When to use this
Date operations are at the heart of most analytics — calculating how many days since a customer’s last purchase, extracting the month from a timestamp for grouping, or formatting dates for a report. Each SQL engine handles dates slightly differently, so this recipe covers the most common operations side by side.Sample input
Anorders table in the Raw layer:
We want to:
- Extract the year and month from
order_date - Calculate the days between order and shipment
- Determine days since the order (relative to today)
Implementation
- Nekt Express / BigQuery
- Athena SQL
- Python (Nekt SDK)
BigQuery uses
EXTRACT, DATE_DIFF, and CURRENT_DATE().Expected output
Assuming today is 2024-12-01:Tips and gotchas
When
shipped_date is NULL (e.g., order 1003 hasn’t shipped yet), all date difference calculations involving that column return NULL. This is the correct behavior — use Handle NULL values if you want to replace it with a default.Timestamps include both date and time. When calculating day differences, most engines ignore the time component and count calendar days. If you need hour-level or minute-level precision, use the appropriate unit (
'hour', 'minute') in SQL or F.unix_timestamp differences in PySpark.