{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 withSELECT serverTimeZone().
Why this happens
ADateTime 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.
DateTimewithout 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 with15:15:32Rome time is before that moment and passes the filter.
DateTime in an ordinary query behaves the same way.
Fix: give the parameter the time zone of the column
Alternatives
Declare the parameter asString. The value is then converted in the comparison, in the time zone of the column, exactly like a literal in the direct query:
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: