Brightlane's workforce analytics platform records which day of the week each support ticket was submitted. A ticket was logged at '2024-03-15 14:32:07'.
Write a query to return the day-of-week number for that timestamp.
Output:
- A single row with one column,
day_of_week, containing the day number where Sunday is0and Saturday is6.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
EXTRACT(
dow
FROM
'2024-03-15 14:32:07'::TIMESTAMP
) AS day_of_week The shape
EXTRACT(dow FROM ...) pulls the day-of-week index out of a datetime and returns a number between 0 and 6. The March 15 timestamp, a Friday, returns 5. The dow field is the standard way to ask which weekday a datetime falls on.
Clause by clause
SELECT EXTRACT(dow FROM '2024-03-15 14:32:07'::timestamp)evaluates the extraction against a single literal. The::timestampcast resolves the string as a timezone-naive datetime. The day-of-week index is the same regardless of the time-of-day component, so14:32:07could be any time during March 15 and the result would still be5.AS day_of_weeklabels the output as the index the support ticket analytics consumes.
The trap
PostgreSQL numbers Sunday as 0 and Saturday as 6. Analysts used to systems where Monday is 0 (ISO 8601) or where Sunday is 1 (spreadsheet tools) will read this index off by one and misclassify weekend tickets as weekday tickets, or vice versa. The off-by-one bug is silent: every value is in the right range, every row gets a label, and the misclassification only surfaces when someone compares a count against a known weekly pattern. The fix is to know the convention before consuming the value: PostgreSQL uses dow = 0 for Sunday, full stop. A separate field, isodow, returns 1 through 7 with Monday as 1 for code that needs the ISO convention.
You practiced EXTRACT(dow FROM ...) — PostgreSQL numbers days from Sunday=0 through Saturday=6, so consumers expecting Monday=0 need an offset.