Skip to main content
A parameter declared as {name:DateTime} has no time zone of its own, so its value is read in the server time zone (or in the session_timezone, if it is set). A string literal compared with a DateTime('Europe/Rome') column is read in the time zone of that column. If the two time zones differ, the parameterized view and the direct query filter by different points in time. Declare the parameter with the time zone of the column, {name:DateTime('Europe/Rome')}, to make them agree.

Reproduce the difference

The outputs below are from a server whose time zone is UTC. Check yours with SELECT serverTimeZone().
A direct query with a string literal returns three rows:
A parameterized view with the same condition returns four rows:

Why this happens

A DateTime value is stored as a point in time, and the time zone only defines how text is converted to and from it.
  • In the direct query, the string '2024-01-01 15:00:00' is converted to the type of the column it is compared with, DateTime('Europe/Rome'). It means 15:00 in Rome.
  • In the view, the value is converted to the type of the parameter first. DateTime without a time zone uses the server time zone, so on a UTC server it means 15:00 UTC, which is 16:00 in Rome. The row with 15:15:32 Rome time is before that moment and passes the filter.
This is not specific to views. A query parameter of type DateTime in an ordinary query behaves the same way.

Fix: give the parameter the time zone of the column

The result no longer depends on the time zone of the server or of the session.

Alternatives

Declare the parameter as String. The value is then converted in the comparison, in the time zone of the column, exactly like a literal in the direct query:
Or keep the view as it is and set session_timezone for the query or the session. The setting changes the implicit time zone of every DateTime value without an explicit time zone in the query, not only of the parameter: