Extract the week from a date in Oracle
How to get a week number from an Oracle DATE: why EXTRACT has no WEEK field, how IW, WW and W differ, the ISO year trap, and when a week-start date works better.
Getting a week number out of a date in Oracle looks like a one-line job, but the result depends on which definition of "week" you pick. Oracle offers several week conventions, and they return different numbers for the same date, especially near the start and end of a year. This review explains what each option produces, where each one fits, and when a plain week-start date is a safer thing to store or share than a bare number.
Short answer: which approach suits your use
For most reporting and grouping, use the ISO week: TO_CHAR(your_date, 'IW') for the week number, paired with TO_CHAR(your_date, 'IYYY') for the matching ISO year. The main benefit is that ISO weeks always start on Monday, always contain seven days, and follow a published international rule, so other systems can reproduce them. The decisive tradeoff is that the ISO year does not always match the calendar year, so a few days in late December or early January belong to a neighboring year's week.
If you need to group rows by week, filter by week, or send the result to someone else, a week-start date from TRUNC(your_date, 'IW') is usually preferable to a week number. A date carries the year and the exact boundary with it, sorts correctly, and cannot be misread as a different week convention.
Why EXTRACT does not work here
Searches for this task often start with EXTRACT, because PostgreSQL and MySQL support something like EXTRACT(WEEK FROM ...). Oracle's EXTRACT function does not accept a WEEK field. Its documented datetime fields are YEAR, MONTH, DAY, HOUR, MINUTE, SECOND and the time zone fields (TIMEZONE_HOUR, TIMEZONE_MINUTE, TIMEZONE_REGION, TIMEZONE_ABBR). A query written for another database with EXTRACT(WEEK ...) will raise an error in Oracle rather than return a number. In Oracle, week numbers come from date format models used with TO_CHAR, and week boundaries come from TRUNC.
What each option actually changes
The two families of functions transform the date in different ways. TO_CHAR converts the date into a character string containing the week number; the original date value is not kept, and the output is VARCHAR2 text, not a number. TRUNC keeps a DATE value but moves it back to the first day of its week, discarding the day-of-week and time-of-day detail. Which one you want depends on whether you need a label or a usable date.
| Expression | Returns | Week definition |
| TO_CHAR(d, 'IW') | Text, '01' to '52' or '53' | ISO 8601 week: weeks start Monday; week 1 is the week containing the year's first Thursday |
| TO_CHAR(d, 'IYYY') | Text, four-digit year | The ISO year that the 'IW' week belongs to |
| TO_CHAR(d, 'WW') | Text, '01' to '53' | Week 1 is January 1 to January 7, week 2 is January 8 to 14, and so on, regardless of weekday |
| TO_CHAR(d, 'W') | Text, '1' to '5' | Week of month: week 1 is days 1 to 7 of the month, regardless of weekday |
| TRUNC(d, 'IW') | DATE | Monday of the ISO week containing d |
| TRUNC(d, 'WW') | DATE | Start of the 'WW' week, which falls on the same weekday as January 1 |
| TRUNC(d, 'DAY') | DATE | First day of the week according to the session's NLS_TERRITORY setting |
The word "week" is the ambiguous part. 'WW' is sometimes assumed to mean a calendar week that starts on Sunday or Monday, but it does neither: it simply counts seven-day blocks from January 1. That means its weeks start on whatever weekday January 1 happens to fall, and its final "week 53" contains only one day, or two in a leap year. 'IW' is the format that matches the ISO week numbers printed in many calendars and used in European business contexts.
SELECT TO_CHAR(DATE '2025-03-14', 'IW') AS iso_week,
TO_CHAR(DATE '2025-03-14', 'IYYY') AS iso_year,
TO_CHAR(DATE '2025-03-14', 'WW') AS jan1_week,
TO_CHAR(DATE '2025-03-14', 'W') AS week_of_month,
TRUNC(DATE '2025-03-14', 'IW') AS iso_week_start
FROM dual;
By the definitions above, March 14, 2025 (a Friday) falls in ISO week 11 of 2025, which begins on Monday, March 10, 2025. It is also in 'WW' week 11 and in week 2 of the month. These values are worked out from the documented format rules, not from a captured query run, so confirm them on your own instance if exact output matters.
The year-boundary trap
The most common mistake is combining the ISO week with the calendar year, for example TO_CHAR(d, 'YYYY') || '-' || TO_CHAR(d, 'IW') or EXTRACT(YEAR FROM d) alongside 'IW'. Near New Year this produces labels that point to the wrong week entirely. Always pair 'IW' with 'IYYY', and 'WW' with 'YYYY'.
| Date | IW | IYYY | WW | YYYY | Note |
| 2024-12-31 (Tuesday) | 01 | 2025 | 53 | 2024 | ISO week 1 of 2025 begins Monday, December 30, 2024 |
| 2021-01-01 (Friday) | 53 | 2020 | 01 | 2021 | Belongs to the last ISO week of 2020 |
In the first row, the mismatched label would be "2024-01", which suggests the first week of January 2024, nearly a year off. The correct ISO label is "2025-01". These values follow from the ISO rule and the weekday each date falls on; they are derived, not copied from a test run.
Settings that can change the result
'IW', 'IYYY', 'WW' and 'W' are defined by fixed rules and do not depend on session language or territory. Day-of-week elements are different: TO_CHAR(d, 'D') and TRUNC(d, 'DAY') use the first day of the week implied by NLS_TERRITORY. With a US territory setting, weeks begin on Sunday; with many European settings, they begin on Monday. Code that relies on 'D' or 'DAY' can therefore return different results for different users or application servers. If you need a Monday-based week independent of session settings, TRUNC(d, 'IW') is the predictable choice.
For TIMESTAMP WITH TIME ZONE values, the week is determined by the time zone stored in or converted for the value. An event late on Sunday in one zone can be early Monday in another, which moves it into a different ISO week. Convert to the reporting time zone before extracting the week.
Grouping and filtering by week
For aggregation, grouping by the week-start date keeps results sortable and unambiguous:
SELECT TRUNC(order_date, 'IW') AS week_start,
COUNT(*) AS orders
FROM orders
GROUP BY TRUNC(order_date, 'IW')
ORDER BY week_start;
For filtering, compare the raw column against a date range instead of wrapping the column in TO_CHAR. A condition such as TO_CHAR(order_date, 'IW') = '11' prevents a normal index on order_date from being used and also ignores the year. A range predicate avoids both problems:
WHERE order_date >= TRUNC(SYSDATE, 'IW')
AND order_date < TRUNC(SYSDATE, 'IW') + 7
If week-number filtering is genuinely required, a function-based index on the same expression is an option, but the range approach is usually simpler.
Will the result stay usable after copying or sharing?
Because TO_CHAR returns text, the output behaves like text once it leaves the database. A few practical consequences follow:
- Leading zeros: 'IW' and 'WW' return two-digit strings such as '05'. Spreadsheet tools commonly convert a pasted '05' to the number 5, and behavior varies by application and import settings, so check the specific destination. If you need a number in SQL, wrap the result in TO_NUMBER.
- Sorting: two-digit week strings sort correctly within a year, and a label such as '2025-W05' built from IYYY and IW sorts correctly across years. A bare week number does not.
- Comparisons: comparing a VARCHAR2 week against a numeric literal forces an implicit conversion. Converting explicitly makes intent clearer and avoids surprises.
- Missing context: a number such as 53 does not say which year or which convention produced it. Someone reading it outside the query may assume a different week system, such as a Sunday-start US week or a spreadsheet's own week function.
- Cross-database moves: week functions in other databases and spreadsheet tools use their own definitions and defaults, so the same date may yield a different number elsewhere. Label exported columns with the convention used, for example "ISO week".
When the value is meant for people rather than further processing, a plain-text equivalent that spells out the period, such as "week of March 10, 2025 (ISO week 11)" or an explicit date range, is easier to read and harder to misinterpret than a code alone. It also reads cleanly with screen readers, which announce a date range in natural language but may read a compact label like "2025-W11" character by character. No assistive-technology testing was supplied for this article, so treat that as a general consideration rather than a verified result for any particular tool.
Gaps in this review
No version-specific test results, benchmarks or captured query output were supplied. The behavior described here reflects Oracle's documented format models and the ISO 8601 week rule, and the example values are derived from those rules. Performance effects of indexing choices and how particular spreadsheets, BI tools or screen readers handle the output were not tested. Verify edge-case dates and your session's NLS settings on your own instance before relying on the results in production.