Unit 4, lesson 1 · EXTRACT, to_char
Pieces of a date: EXTRACT and to_char
The month of each cancelled order
order_date is a date column. EXTRACT(part FROM date) pulls out one piece as a number: EXTRACT(YEAR FROM DATE '2026-03-17') is 2026, and EXTRACT(MONTH FROM DATE '2026-03-17') is 3. (Writing DATE in front of a quoted value marks it as a date.) to_char(date, pattern) writes a date as text in the pattern you choose, where YYYY is the year, MM the month and DD the day: to_char(DATE '2026-03-17', 'YYYY-MM') gives '2026-03'.
SELECT order_date,
EXTRACT(YEAR FROM order_date) AS order_year,
to_char(order_date, 'DD') AS day_of_month
FROM orders
LIMIT 5;Your task
Finance is reviewing this year's cancellations by month. For every order placed in 2026 with the status cancelled, return the id, the order_date and the month it was placed in, written like '2026-03', as order_month.
Tables you will use: orders
Try it in your browserFree, no sign-up. Your query is checked as you go.