Date_sub ok with mysql, ko with postgresql

This query, which works with mySQL, does not work with Postgresql:

select ... from ... where (id = ... and ( h > date_sub(now(), INTERVAL 30 MINUTE))) 

Error:

 Query failed: ERREUR: erreur de syntaxe sur ou prรจs de ยซ 30 ยป 

Any ideas?

+4
source share
3 answers

DATE_SUB is a MySQL function that does not exist in PostgreSQL.

You can (for example) use:

 NOW() - '30 MINUTES'::INTERVAL 

... or...

 NOW() - INTERVAL '30' MINUTE 

... or...

 NOW() - INTERVAL '30 MINUTES' 

as a replacement.

SQLfiddle with all 3 to test with .

+13
source

For the interval literal, single quotes are needed:

 INTERVAL '30' MINUTE 

And you can use the usual "arithmetic":

 and (h > current_timestamp - interval '30' minute) 
0
source

Try using something like:

 select ... from ... where id = ... and h > now() - INTERVAL '30 MINUTE' 
0
source

All Articles