Part 104: Date Extraction & Arithmetic: EXTRACT & Date Math
Working with calendar intervals and calculating user age or tenure.
1. Date Arithmetic in PostgreSQL
-- Add 14 days to a trial period:
SELECT signup_date + INTERVAL '14 days' AS trial_expiration
FROM accounts;
-- Extracting sub-components:
SELECT
EXTRACT(YEAR FROM created_at) AS signup_year,
EXTRACT(DOW FROM created_at) AS day_of_week -- 0 = Sunday
FROM accounts;
Key Takeaways
- Use
EXTRACT(FIELD FROM timestamp)for granular date parts.
In Part 105, we explore Formatting Dates & Numbers: TO_CHAR & DATE_FORMAT!

