Brightlane's billing system routes transactions to the correct monthly processing bucket based on the month number of the transaction timestamp.
Write a query to return the month number for the timestamp '2024-08-22 09:15:00'.
Output:
- A single row with one column,
month_number, containing the month as a number.
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(
MONTH
FROM
'2024-08-22 09:15:00'::TIMESTAMP
) AS month_number The shape
EXTRACT(month FROM ...) pulls the month number out of a datetime and returns a value between 1 and 12. The August timestamp returns 8, which the billing system routes to the eighth monthly processing bucket.
Clause by clause
SELECT EXTRACT(month FROM '2024-08-22 09:15:00'::timestamp)evaluates the extraction against a single literal. The::timestampcast resolves the string as a timezone-naive datetime. The month component is the same whether the input is adate, atimestamp, or atimestamptz, so the cast choice doesn't affect the value, only the input type.AS month_numberlabels the result as the bucket number the routing system reads.
The trap
The returned value carries no year. EXTRACT(month FROM '2024-08-22') and EXTRACT(month FROM '2023-08-22') both return 8. For a routing decision that already lives inside a single billing period this is exactly the right answer, but the same expression on a multi-year dataset collapses August 2023 and August 2024 into the same number. Knowing the value is decoupled from the year is what tells a reader whether the expression is being used for a within-period decision or whether it is silently dropping the year dimension.
You practiced EXTRACT(month FROM ...) — pull the month number (1–12) out of a datetime, with no year attached to the result.