#28297·directus

weekday() returns a different numbering on every database vendor

Author: galshirCreated Sep 25, 2026Updated Sep 29, 2026
LabelsBugQueryingEngineBackend

Describe the Bug

weekday() is a public query function - it can be used in fields, filter, groupBy, sort and aggregations - but each dialect helper maps it onto a different vendor function, and those functions do not share a numbering scheme. The same query therefore returns different numbers, and matches different days, depending on which database backs the project.

From api/src/database/helpers/fn/dialects/:

dialect expression numbering
Postgres EXTRACT(DOW FROM ...) 0 = Sunday ... 6 = Saturday
SQLite strftime('%w', ...) 0 = Sunday ... 6 = Saturday
MySQL / MariaDB DAYOFWEEK(...) 1 = Sunday ... 7 = Saturday
MS SQL DATEPART(weekday, ...) 1 ... 7, offset by SET DATEFIRST (session dependent)
Oracle TO_CHAR(..., 'D') 1 ... 7, first day of week depends on NLS_TERRITORY (session dependent)

So a filter of weekday(date_created) _eq 1 selects Monday on Postgres and SQLite, Sunday on MySQL, and on Oracle and MS SQL it depends on a server setting rather than on the data.

That has two consequences. A filter, group, sort or flow condition written against one database silently selects the wrong day after a move to another vendor - nothing errors, the numbers are just shifted. And on Oracle and MS SQL the result is not deterministic from Directus' point of view at all: the same rows and the same query return different numbers depending on NLS_TERRITORY / DATEFIRST.

week() has a smaller version of the same problem: Postgres EXTRACT(WEEK) and Oracle IW are both ISO weeks, but SQLite %W is a Monday-based 00-53 count and MySQL WEEK() defaults to mode 0 (Sunday based, 0-53), so those two disagree with ISO around year boundaries.

This is the follow-up I flagged in #28257, where @Nitwel asked to make sure the other date part functions were covered too. That PR standardised the return type on Oracle (TO_NUMBER), but deliberately left the weekday numbering alone, because choosing a convention is a behaviour change rather than a bug fix.

To Reproduce

Being upfront: this is a code-level report. I read it out of the dialect helpers rather than standing up five databases, so the table above is the evidence and the steps below are what I would expect to see. Happy to be corrected if a vendor behaves differently than its documentation says.

  1. Create a collection with a datetime field and one row whose value is a known Monday, e.g. 2026-01-05T12:00:00.
  2. Request the weekday: GET /items/:collection?fields=weekday(date_field)
  3. Run the identical request against the identical data on Postgres, MySQL and SQLite.
  4. Expected: the same number for the same day on every vendor. Actual, per the expressions above: 1 on Postgres and SQLite, 2 on MySQL, and on Oracle / MS SQL whatever the session's first-day-of-week setting makes it.

What I would like to settle before writing any code: which numbering do you want as the canonical one? I can see three reasonable answers and they are not equally cheap.

ISO 8601 (1 = Monday ... 7 = Sunday) is the most standard and lines up with week() already being ISO on Postgres and Oracle, but it changes the numbers on every dialect, which breaks existing filters and stored flow conditions.

Postgres DOW (0 = Sunday ... 6 = Saturday) leaves the two most common backends alone and only shifts MySQL, MS SQL and Oracle.

Or leave the numbers as they are and only pin the locale-dependent ones, so Oracle and MS SQL stop depending on server settings, then document the remaining vendor difference.

My own preference is the second: smallest blast radius, and it still makes Oracle and MS SQL deterministic. But it is visible in every existing filter, so I would rather not pick unilaterally. Happy to implement whichever you choose, including e2e coverage across the dialects in tests/ - just say which one and I will open the PR.

AI disclosure: I used Claude as an assistant while tracing these helpers. I read the source myself and stand behind the table.

Directus Version

main (dialect helpers at 9f50ad4)

Hosting Strategy

Self-Hosted (Custom)

Database

All supported vendors - Postgres, MySQL/MariaDB, SQLite, MS SQL, Oracle (code-level report)