0

Postgres here; I have a Users table that has the following fields:

create table Users ( 
    id bigserial primary key, 
    isAdmin boolean not null default false, 
    beginDate timestamp with time zone not null, 
    updated timestamp with time zone
);

I want to write a query that fetches any Users records that:

  • Have a beginDate value within the last 24 hours (inclusively); AND
  • Have an updated value that is older (exclusively) than 24 hours

My best attempt thus far is:

select *
from
Users
where
beginDate >= NOW() - INTERVAL 1 DAY and
updated < NOW() - INTERVAL 1 DAY

But this gives em the following error:

ERROR: syntax error at or near "1"
  Position: 59

beginDate >= NOW() - INTERVAL 1 DAY and
                              ^
1 statement failed.

Execution time: 0.03s

Any ideas on what the fix is?

1
  • 1
    INTERVAL '1' DAY Commented Apr 10, 2019 at 19:51

1 Answer 1

5

The correct syntax would be this:

beginDate >= NOW() - INTERVAL '1 DAY' and
updated < NOW() - INTERVAL '1 DAY'

You can find more information here: https://www.postgresql.org/docs/current/functions-datetime.html

Sign up to request clarification or add additional context in comments.

3 Comments

Still wrong... Use straight quotes, for the number only. See my comment.
@jarlh . . . This is correct. Postgres accepts both.
Oops, I see. Sorry, @matias!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.