2

I have the following query in Postgres:

SELECT * FROM users WHERE registration = last_login;

Both registration and last_login are timestamps. The problem I have is that those two values are written with some tiny difference, but they are not exact. Is there any way I can compare those two dates with a margin of, say, 1 second?

2 Answers 2

6

a_horse_with_no_name actualy has answered your question but just for case when there's no guarantee one date is after another you could try something like this:

SELECT *
FROM users
WHERE @extract(epoch from registration - last_login) <= 1;

extract(epoch from some_interval) returns timestamp difference in seconds as a number, and @some_value returns absolute value of a number, just like abs(some_value)

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

Comments

1

Assuming that last_login is always after registration, you can check the difference between the two timestamps

select *
from users
where last_login - registration <= interval '1' second

Comments

Your Answer

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