Search code examples
postgresqltimestamppg

Syntax error while comparing timestamps in PostgreSQL


Error: "syntax error at or near interval"

Join in question:

... LEFT OUTER JOIN usersession e ON a.userID = e.userID AND e.lastAction >= now() interval '-4h' * 1"

lastAction was created as lastAction timestamp NULL DEFAULT NULL

The idea is, to return some nils if the session's timestamp is 4 hours older than the current time.

No idea what the deal is here, never had to compare timestamps before either.


Solution

  • If you want records for the last 4 hours you need:

    now() - interval '4 hours'
    

    instead of

    now() interval '-4h' * 1