Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date_sub ok with mysql, ko with postgresql

Tags:

sql

postgresql

This query which works with mySQL doesn't work with Postgresql:

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

The error is:

Query failed: ERREUR:  erreur de syntaxe sur ou près de « 30 »  

Any ideas ?

like image 611
Bertaud Avatar asked May 01 '13 14:05

Bertaud


1 Answers

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

You can (for example) either use;

NOW() - '30 MINUTES'::INTERVAL

...or...

NOW() - INTERVAL '30' MINUTE

...or...

NOW() - INTERVAL '30 MINUTES'

as a replacement.

An SQLfiddle with all 3 to test with.

like image 179
Joachim Isaksson Avatar answered Nov 06 '22 09:11

Joachim Isaksson