1

Using this query:

users = User.where('confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL ? days)', 1)

In mysql goes okay but in Postgresql it fails with:

PG::SyntaxError: ERROR:  syntax error at or near "1"
    LINE 1: ...AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day))
                                                                 ^
    : SELECT "users".* FROM "users"
      WHERE (confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day))

I'm trying to understand but missing the context here. Why is the integer 1 not valid in this query?

2 Answers 2

3

There is no function DATE_SUB() in PostgreSQL, so it cannot work.

This expression would work in Postgres:

... AND confirmation_sent_at <= (now() - interval '1 day')

Or, if confirmation_sent_at is a date:

... AND confirmation_sent_at <= (now()::date - 1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I totally missed that DATE_SUB() could have be a mysql function only a bit cleaner than the one below
1

Try...

users = User.where(
    "confirmed_at IS NULL " +
    "AND confirmation_sent_at <= (NOW() - INTERVAL '1 DAY')"
)

Comments

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.