Extracting Date Components from Dates
Often times when working with time series data for analysis, you may be required to extract information from dates such a
the quarter, month, year or even day of the week. For example, analyzing weekend sales may be an interesting exercise to
understand business performance behavior. This short snippet demonstrates how to extract such components from date.
enrollments table
For this exercise, I will use the enrollments table which looks like below
SELECT * FROM enrollments
Extract() Function
Postgresql offers the Extract() function to help us deal with dates. We can simply extract the date
by passing the Year from and the date column.
For example:
SELECT EXTRACT(year from '2025-04-01'::date)
Extract Year from enrollments
Now let's put that together in a query on enrollments table
SELECT enrollment_date,
EXTRACT(year from enrollment_date) AS year,
completion_status
FROM enrollments;