Use intervals ==> Date/Time Functions and Operators
create table asd(
name varchar(20),
last_sync timestamp
)
;
insert into asd values( 'one', now() - interval '500' hour );
insert into asd values( 'two', now() - interval '1500' hour );
SELECT *
from asd
where NOW() - last_sync > ( 86400 * 30 ) * INTERVAL '1' second
name |last_sync |
-----|--------------------|
two |2016-10-26 00:52:16 |
How to make it work if last_sync is -infinity? – Fomalhaut 5 mins ago
insert into asd values( 'one', now() - interval '500' hour );
insert into asd values( 'two', now() - interval '1500' hour );
insert into asd values( 'minus infinity', timestamp '-infinity' );
insert into asd values( 'plus infinity', timestamp 'infinity' );
SELECT *
from asd
where last_sync > NOW() - ( 86400 * 30 ) * INTERVAL '1' second
name |last_sync |
--------------|-------------------------|
one |2016-12-06 15:52:12 |
plus infinity |292278994-08-17 00:00:00 |