--- title: "yearweek" slug: "dev-database-reference-sql-functions-date-time-yearweek" updated: 2026-10-30T18:19:01Z published: 2026-10-30T18:19:01Z canonical: "docs.vastdata.com/dev-database-reference-sql-functions-date-time-yearweek" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.vastdata.com/llms.txt > Use this file to discover all available pages before exploring further. # yearweek *Applies to VAST Cluster 5.5.* *[Functions](/develop/docs/dev-database-reference-sql-functions-index) › [Date and time functions](/develop/docs/dev-database-reference-sql-functions-date-time) › Weeks and days of the week.* Returns the ISO 8601 year and week of a date or timestamp as one number: the year times 100 plus the week, such as `202609` for week 9 of 2026. ## Syntax ``` yearweek(value) ``` | Argument | Type | Description | | --- | --- | --- | | `value` | `DATE` or `TIMESTAMP` (also `TIMESTAMP_NS`, `TIMESTAMP_MS`, `TIMESTAMP_S`) | The value to take the ISO year and week of. | **Returns:** `BIGINT`. **Same as:** `date_part('yearweek', value)`; see [date_part](/develop/docs/dev-database-reference-sql-functions-date-time-date-part). ## Set up the examples The examples connect with the ADBC driver, as in [VAST Query Engine (ADBC)](/develop/docs/dev-database-integrations-adbc), and work in a schema called `docs_sql_fn_date_time_yearweek`. Set `QE_ENDPOINT`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `VASTDB_BUCKET` ([SQL reference](/develop/docs/dev-database-reference-sql-index#example-connect-and-use-a-transaction)), and run the blocks in order in one Python session. Below each block is the output you should see. This block connects, defines the `run()` and `plan()` helpers, and creates a table of seven events. Each has a date (`d`), a timestamp (`ts`), a time of day (`t`) and a timestamp with nanoseconds (`tns`). Row 4 has no values, row 6 is the last moment of 1969, and row 7 is a Sunday early in January 2027: ``` import os import re import adbc_driver_manager import adbc_driver_manager.dbapi as dbapi import adbc_driver_vastdb BUCKET = os.environ["VASTDB_BUCKET"] SCHEMA = f'"{BUCKET}/docs_sql_fn_date_time_yearweek"' conn = dbapi.connect( driver=adbc_driver_vastdb.get_driver_path(), db_kwargs={ "vast.db.endpoint": os.environ["QE_ENDPOINT"], "vast.db.access_key": os.environ["AWS_ACCESS_KEY_ID"], "vast.db.secret_key": os.environ["AWS_SECRET_ACCESS_KEY"], }, autocommit=True, ) def server_message(e): """The server's message in a driver error, without the request ID or the echoed statement.""" text = re.sub(r"\[req=0x[0-9a-f]+\]: ", "", str(e)) found = (re.search(r'message: "(.*?)", request_id', text, re.S) or re.search(r"description: (.*)\)", text, re.S)) message = found.group(1) if found else text.strip().splitlines()[-1] message = re.sub(r"^Plan query: '.*?' failed with: ", "", message, flags=re.S) message = re.split(r"\\n|\n", message)[0].replace('\\"', '"') return message.replace(BUCKET, "") def run(sql): """Run one statement. Print the rows it returns, or the error.""" try: with conn.cursor() as cur: cur.execute(sql) rows = cur.fetchall() if cur.description else [] except (adbc_driver_manager.Error, OSError) as e: print(f"{type(e).__name__}: {server_message(e)}") else: print(rows) def plan(sql): """Print the plan without its ~N Rows lines: the table's row count, which can still be 0 just after an insert.""" with conn.cursor() as cur: cur.execute("EXPLAIN " + sql) steps = cur.fetchall()[0][1] print("\n".join(line for line in steps.splitlines() if "Rows" not in line)) TABLE = f"{SCHEMA}.events" run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}") run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, d DATE, ts TIMESTAMP, t TIME, tns TIMESTAMP_NS)") run(f"INSERT INTO {TABLE} VALUES " "(1, DATE '2026-02-28', TIMESTAMP '2026-02-28 23:15:00', TIME '23:15:00', " "CAST('2026-02-28 23:15:00.123456789' AS TIMESTAMP_NS)), " "(2, DATE '2026-03-01', TIMESTAMP '2026-03-01 08:30:45.123456', TIME '08:30:45.123456', " "CAST('2026-03-01 08:30:45.123456' AS TIMESTAMP_NS)), " "(3, DATE '2024-12-30', TIMESTAMP '2024-12-30 12:00:00', TIME '12:00:00', " "CAST('2024-12-30 12:00:00' AS TIMESTAMP_NS)), " "(4, NULL, NULL, NULL, NULL), " "(5, DATE '2024-02-29', TIMESTAMP '2024-02-29 00:00:00', TIME '00:00:00', " "CAST('2024-02-29 00:00:00' AS TIMESTAMP_NS)), " "(6, DATE '1969-12-31', TIMESTAMP '1969-12-31 23:59:59.999999', TIME '23:59:59.999999', " "CAST('1969-12-31 23:59:59.999999999' AS TIMESTAMP_NS)), " "(7, DATE '2027-01-03', TIMESTAMP '2027-01-03 00:00:00.5', TIME '00:00:00.000001', " "CAST('2027-01-03 00:00:00.5' AS TIMESTAMP_NS))") ``` ``` [] [] [(7,)] ``` ## What it does `yearweek` combines [isoyear](/develop/docs/dev-database-reference-sql-functions-date-time-isoyear) and [week](/develop/docs/dev-database-reference-sql-functions-date-time-week): `isoyear(value) * 100 + week(value)`. ISO 8601 weeks start on Monday, and week 1 of a year is the week that holds the year's first Thursday. So the first days of January can belong to the last week of the year before, and the last days of December to week 1 of the next year: | Date | Day | Result | Why | | --- | --- | --- | --- | | `DATE '2026-01-01'` | Thursday | `202601` | The first Thursday of 2026 is in week 1. | | `DATE '2024-12-30'` | Monday | `202501` | The week holds 2 January 2025, a Thursday, so it's week 1 of 2025. | | `DATE '2021-01-01'` | Friday | `202053` | The week started on Monday 28 December 2020: week 53 of 2020. | | `DATE '2027-01-03'` | Sunday | `202653` | The last day of week 53 of 2026. | ## NULLs and edge cases | Input | Result | | --- | --- | | A NULL value (row 4) | `NULL` | | `DATE 'infinity'`, `TIMESTAMP '-infinity'` | `NULL` | | `DATE '0001-01-01'` | `101`: the year isn't padded, so the number has fewer digits | | `DATE '9999-12-31'` | `999952` | | `CAST('0044-03-15 (BC)' AS DATE)` | `-4311`: year -43, week 11 | | `TIMESTAMP '1969-12-31 23:59:59.999999'` | `197001` | | An untyped `NULL`: `yearweek(NULL)` | Error: `Could not choose a best candidate function for the function call "yearweek("NULL")"` | | An `INTERVAL` | Error: `interval units "week" not recognized` | | A `TIME` | Error: `No function matches the given name and argument types 'yearweek(TIME)'` | | A string | Error: `Could not choose a best candidate function`; cast it to `DATE` first | | A number, boolean, `BLOB` or list | Error: `No function matches the given name and argument types`, for example `'yearweek(INTEGER_LITERAL)'` | The same cases, as the cluster returns them. They use the table and helpers from [Set up the examples](/develop/docs/dev-database-reference-sql-functions-date-time-yearweek#set-up-the-examples): ``` run(f"SELECT id, yearweek(ts) FROM {TABLE} WHERE id = 4") run("SELECT yearweek(DATE '2026-01-01'), yearweek(DATE '2024-12-30'), yearweek(DATE '2021-01-01'), " "yearweek(DATE '2027-01-03')") run("SELECT yearweek(DATE 'infinity'), yearweek(TIMESTAMP '-infinity'), yearweek(DATE '0001-01-01'), " "yearweek(DATE '9999-12-31'), yearweek(CAST('0044-03-15 (BC)' AS DATE)), " "yearweek(TIMESTAMP '1969-12-31 23:59:59.999999')") run("SELECT yearweek(NULL)") run("SELECT yearweek(INTERVAL '1 year 2 months 3 days')") run("SELECT yearweek(TIME '13:45:30')") run("SELECT yearweek('2026-03-01')") run("SELECT yearweek(123)") ``` ``` [(4, None)] [(202601, 202501, 202053, 202653)] [(None, None, 101, 999952, -4311, 197001)] InternalError: Binder Error: Could not choose a best candidate function for the function call "yearweek("NULL")". In order to select one, please add explicit type casts. OSError: interval units "week" not recognized InternalError: Binder Error: No function matches the given name and argument types 'yearweek(TIME)'. You might need to add explicit type casts. InternalError: Binder Error: Could not choose a best candidate function for the function call "yearweek(STRING_LITERAL)". In order to select one, please add explicit type casts. InternalError: Binder Error: No function matches the given name and argument types 'yearweek(INTEGER_LITERAL)'. You might need to add explicit type casts. ``` ## Examples The ISO year and week of each event. Row 3 (30 December 2024) is in week 1 of 2025, and row 6 (31 December 1969) in week 1 of 1970: ``` run(f"SELECT id, d, yearweek(d), yearweek(ts) FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, datetime.date(2026, 2, 28), 202609, 202609), (2, datetime.date(2026, 3, 1), 202609, 202609), (3, datetime.date(2024, 12, 30), 202501, 202501), (4, None, None, None), (5, datetime.date(2024, 2, 29), 202409, 202409), (6, datetime.date(1969, 12, 31), 197001, 197001), (7, datetime.date(2027, 1, 3), 202653, 202653)] ``` The same week as a label in the ISO format, such as `2026-W09`, with the `%G` (ISO year) and `%V` (ISO week) codes of [strftime](/develop/docs/dev-database-reference-sql-functions-date-time-strftime): ``` run(f"SELECT id, strftime(d, '%G-W%V') FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, '2026-W09'), (2, '2026-W09'), (3, '2025-W01'), (4, None), (5, '2024-W09'), (6, '1970-W01'), (7, '2026-W53')] ``` Keep only the events in week 9 of 2026: ``` run(f"SELECT id FROM {TABLE} WHERE yearweek(d) = 202609 ORDER BY id LIMIT 10") ``` ``` [(1,), (2,)] ``` ## How it runs in VAST The Query Engine runs on the cluster's CNodes, and a query on a table starts with a scan in VAST DataBase (`VAST_SCAN`). VAST DataBase can evaluate a comparison of a column with a constant, but not a function of a column, so `yearweek()` is always computed by the Query Engine: - **In the `SELECT` list,** the scan reads the column and the Query Engine computes `yearweek()` for each row it returns. - **In `WHERE`,** the scan sends the column for every row, and a `FILTER` step in the Query Engine computes `yearweek()` and keeps the rows that match. No rows are skipped in storage. The plan of the `WHERE` example shows the `FILTER` step above the scan: ``` plan(f"SELECT id FROM {TABLE} WHERE yearweek(d) = 202609") ``` ``` ┌───────────────────────────┐ │ PROJECTION │ │ ──────────────────── │ │ #1 │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ──────────────────── │ │ (yearweek(d) = 202609) │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ │ │ Projections: │ │ d │ │ id │ │ │ └───────────────────────────┘ ``` ## Make it faster - **For one week, filter on a range of the raw column.** Week 9 of 2026 runs from Monday 23 February to Sunday 1 March, so `d >= DATE '2026-02-23' AND d < DATE '2026-03-02'` selects the same rows. The range is pushed down to VAST DataBase, so rows outside it never leave storage. The plan for the range has no `FILTER` step: ``` plan(f"SELECT id FROM {TABLE} WHERE d >= DATE '2026-02-23' AND d < DATE '2026-03-02'") ``` ``` ┌───────────────────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ Projections: id │ │ │ └───────────────────────────┘ ``` - **Find the week's Monday with `date_trunc`.** `date_trunc('week', d)` returns the Monday that starts the ISO week, which gives you the start of the range; see [date_trunc](/develop/docs/dev-database-reference-sql-functions-date-time-date-trunc). - **Store the week if you filter on many weeks.** Compute `yearweek` when you write the rows and keep it in its own column: `event_week = 202609` is pushed down, `yearweek(d) = 202609` isn't. ## Limitations and errors - **ISO weeks only.** `yearweek` always uses ISO 8601 weeks, which start on Monday. For weeks that start on Sunday, use the `%U` code of [strftime](/develop/docs/dev-database-reference-sql-functions-date-time-strftime). - **No time-zone support.** On a `TIMESTAMP WITH TIME ZONE` value, such as `now()`, `yearweek` fails with an `Extension Autoloading Error`. Cast the value to `TIMESTAMP` first; see [time zones](/develop/docs/dev-database-reference-sql-functions-date-time-time-zones). - **Not for `TIME` or `INTERVAL`.** The error for an `INTERVAL` lists `yearweek(INTERVAL)` among the candidates, but running it fails with `interval units "week" not recognized`. ## Similar functions | Function | Returns | Result for `DATE '2024-12-30'` (`yearweek` gives `202501`) | | --- | --- | --- | | [week](/develop/docs/dev-database-reference-sql-functions-date-time-week) | The ISO week, 1 to 53 | `1` | | [isoyear](/develop/docs/dev-database-reference-sql-functions-date-time-isoyear) | The ISO year the week belongs to | `2025` | | [year](/develop/docs/dev-database-reference-sql-functions-date-time-year) | The calendar year | `2024` | | [strftime](/develop/docs/dev-database-reference-sql-functions-date-time-strftime) with `'%G%V'` | The ISO year and week, as text | `'202501'` | | [date_part](/develop/docs/dev-database-reference-sql-functions-date-time-date-part) with `'yearweek'` | The same as `yearweek` | `202501` | ``` run("SELECT yearweek(DATE '2024-12-30'), week(DATE '2024-12-30'), isoyear(DATE '2024-12-30'), " "year(DATE '2024-12-30'), strftime(DATE '2024-12-30', '%G%V'), date_part('yearweek', DATE '2024-12-30')") ``` ``` [(202501, 1, 2025, 2024, '202501', 202501)] ``` Clean up: ``` run(f"DROP TABLE IF EXISTS {TABLE}") run(f"DROP SCHEMA IF EXISTS {SCHEMA}") conn.close() ``` ``` [] [] ``` ## See also - [week](/develop/docs/dev-database-reference-sql-functions-date-time-week) and [isoyear](/develop/docs/dev-database-reference-sql-functions-date-time-isoyear): the two halves of `yearweek`. - [Date and time functions](/develop/docs/dev-database-reference-sql-functions-date-time): all the date and time functions. - [Where filters run](/develop/docs/dev-database-reference-sql-select#where-filters-run) and [pushdowns](/develop/docs/dev-database-reference-pushdowns). - [EXPLAIN](/develop/docs/dev-database-reference-sql-explain): how to read a plan.