date_sub ok with mysql, ko with postgresql
NickName:Bertaud Ask DateTime:2013-05-01T22:16:09

date_sub ok with mysql, ko with 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 ?

Copyright Notice:Content Author:「Bertaud」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16319804/date-sub-ok-with-mysql-ko-with-postgresql

Answers
Joachim Isaksson 2013-05-01T14:24:42

DATE_SUB is a MySQL function that does not exist in PostgreSQL.\n\nYou can (for example) either use;\n\nNOW() - '30 MINUTES'::INTERVAL\n\n\n...or...\n\nNOW() - INTERVAL '30' MINUTE\n\n\n...or...\n\nNOW() - INTERVAL '30 MINUTES'\n\n\nas a replacement.\n\nAn SQLfiddle with all 3 to test with.",


a_horse_with_no_name 2013-05-01T14:21:16

An interval literal needs single quotes:\n\nINTERVAL '30' MINUTE\n\n\nAnd you can use regular \"arithmetics\":\n\nand (h > current_timestamp - interval '30' minute)\n",


More about “date_sub ok with mysql, ko with postgresql” related questions

date_sub ok with mysql, ko with 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: erre...

Show Detail

mysql match by date using date_sub

I have this mysql query SELECT * FROM all_sent_Orders a WHERE a.nachforschungCompletedOn IS NULL AND CASE WHEN a.lastMessageSent IS NOT NULL AND a.lastResponse IS NULL THEN date(da...

Show Detail

MySQL query fails in PostgreSQL

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:

Show Detail

MySQL: Convert from Varchar to Date and applying Date_Sub

I am trying to use 'date-sub' to subtract few month using MySQL. Before I do that, I figure I have to covert varchar(7) value into date format first. So, I used following: select str_to_date('7-20...

Show Detail

MySQL - Search Query between DATE_SUB() AND DATE_SUB() Error

I am trying to do a search query (using pdo php and mysql) between a date range which is between (date-7days) and (date-1day) using DATE_SUB() on both so for example: end_date is 2013-03-26 search

Show Detail

mysql date_sub using a field as interval

I need help with mysql and date_sub(). I have a table call Activity Activity(id,deadline,alert) Activity(1,'2011-04-18','1 DAY'); Activity(2,'2011-04-13','1 MONTH'); Every row in A have an

Show Detail

Jmeter samples Ok and Ko doesnot match

I have run a web test and successfully generated the Apdex report but I see the Ok and Ko counts doesn't match. There are 10 transactions in my case, when I SUM the OK count of transaction-2 with KO

Show Detail

How can use mysql DATE_SUB in laravel?

How can use mysql date_sub method in laravel? In laravel I want to get the value of time property using date_sub. and I tried the code below but and I can not get anything. $notAllowedTime = DB::...

Show Detail

MySql query syntax error DATE_SUB

what's wront with this query? SELECT * FROM containmentTracker WHERE reviewDate < NOW() AND reviewDate > DATE_SUB(NOW(), INTERVAL 10 YEARS) I've tried in several way but every time I use

Show Detail

Error in MySQL to HiveQL Conversion including DATE_SUB and INTERVAL

I am trying to convert the query written in MySQL to HiveQL The part of query giving me error is : WHERE 1 = 1 AND table1.incoming_date >= DATE_SUB(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE()) -...

Show Detail