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 ?
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.
An interval literal needs single quotes:
INTERVAL '30' MINUTE
And you can use regular "arithmetics":
and (h > current_timestamp - interval '30' minute)
Try using something like :
select ...
from ...
where id = ...
and h > now() - INTERVAL '30 MINUTE'
Related
how can I select in MariaDB between two dates (in this case between two minutes)? I mean I want to select between from now() +5 minutes and now() + 30 minutes.
I tried with this query but no luck.
SELECT req_id FROM info WHERE date(sent_date) BETWEEN (NOW() - INTERVAL 5 MINUTE) AND date(sent_date) < (NOW() - INTERVAL 30 MINUTE)
Thank you so much for helping.
BTW, I tried to search answer to my question in stackoverflow but I don't found.
The syntax of your WHERE clause is off. Use this version:
SELECT req_id
FROM info
WHERE sent_date BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW() - INTERVAL 30 MINUTE;
im trying to get a query working that will show the last 90 days of data. this is my timestamp code(it was a unix timestamp) :
"" to_date('1970-01-01','YYYY-MM-DD') +
numtodsinterval(c.f_crtm,'SECOND')- 6/24 as "oracle date" ""
this is the format of my date when the above code is run: 21-JUN-2020 15:48:59
i know it has something to do with the SYSDATE -90 but i cant figure it out.
any help would be great.
I am thinking of something like this:
(date '1970-01-01' + c.f_crtm * interval '1' second) - interval '6' hour > sysdate - interval '90' day
Or to use an index on (f_crtm), you can rearrange the logic:
c.f_crtm > (trunc(sysdate - interval '90' day + interval '6 hour') - date '1970-01-01')*24*60*60
I am working on queries in Amazon RedShift.
I have created in query in which I am getting syntax error but I am unable to know the issue behind it. The query looks OK to me.
Below is the query:
select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) * INTERVAL '1 second') as eta
where eta >= (SELECT NOW() - INTERVAL '1 DAY')
from trips;
Here, _action_date is a bigint integer so I am converting it into timestamp format also.
I am trying to extract those rows in which _action_date is greater than or equal to yesterday date.
The error I am getting:
Error running query: syntax error at or near "from" LINE 1: ...ta where eta >= (SELECT NOW() - INTERVAL '1 DAY') from trips... ^
Please help me in correcting it. Any help would be highly appreciated.
select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) *
INTERVAL '1 second') as eta
from trips
where eta >= (SELECT getdate() - INTERVAL '1 DAY');
select *
From #####
where username in ('akhil') and between (now() ::timestamp and now() - interval '1 day'::timestamp)
Getting error in this line .
Error - cannot cast type interval to timestamp without time zone .
You don't need the second cast. You need a column name. Something like this:
select *
from #####
where username in ('akhil') and
<some date column> >= now() - interval '1 day' and
<some date column> < now();
the error is in the second cast
now() - interval '1 day'::timestamp
is interprested as.
now() - (interval '1 day')::timestamp
when you actually mean
(now() - interval '1 day')::timestamp
or possibly you mean
now()::timestamp - interval '1 day'
both are valid, but the the result you'll get when daylight saving starts or stops is different (hint interval '1 day' is the same as interval '24 hours' in the first)
There's a strong possiblity that you should actually be using timestamp with time zone for the database column.
SELECT Table.date FROM Table WHERE date > current_date - 10;
Does this work on PostgreSQL?
Yes this does work in PostgreSQL (assuming the column "date" is of datatype date)
Why don't you just try it?
The standard ANSI SQL format would be:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10' day;
I prefer that format as it makes things easier to read (but it is the same as current_date - 10).
http://www.postgresql.org/docs/current/static/functions-datetime.html shows operators you can use for working with dates and times (and intervals).
So you want
SELECT "date"
FROM "Table"
WHERE "date" > (CURRENT_DATE - INTERVAL '10 days');
The operators/functions above are documented in detail:
CURRENT_DATE
INTERVAL '10 days'
My understanding from my testing (and the PostgreSQL dox) is that the quotes need to be done differently from the other answers, and should also include "day" like this:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10 day';
Demonstrated here (you should be able to run this on any Postgres db):
SELECT DISTINCT current_date,
current_date - interval '10' day,
current_date - interval '10 days'
FROM pg_language;
Result:
2013-03-01 2013-03-01 00:00:00 2013-02-19 00:00:00
The suggested answers already seem to solve the questions. But as an addition I am suggesting to use the NOW() function of PostgreSQL.
SELECT Table.date
FROM Table
WHERE date > now() - interval '10' day;
Additionally you can even specifiy the time zone which can be really handy.
NOW () AT TIME ZONE 'Europe/Paris'
Starting with Postgres 9.4 you can use the AGE function:
SELECT Table.date FROM Table WHERE AGE(Table.date) <= INTERVAL '10 day';
Just generalising the query if you want to work with any given date instead of current date:
SELECT Table.date
FROM Table
WHERE Table.date > '2020-01-01'::date - interval '10 day'
I would check datatypes.
current_date has "date" datatype, 10 is a number, and Table.date - you need to look at your table.
you can use between too:
SELECT Table.date
FROM Table
WHERE date between current_date and current_date - interval '10 day';