I am working with PostgreSQL 12.6 and TimescaleDB.
I have an hypertable raws which contains a field "time" of type timestamp without time zone NOT NULL.
Now I have 2 queries:
select * from raws where time > NOW() - INTERVAL '15 day';
This one gives me this:
2021-04-08 00:00:00
2021-04-08 00:10:00
2021-04-08 00:20:00
And NOW() is equal to: 2021-04-20 16:48:41.575584+02
But now, if I take the value of NOW() - INTERVAL '15 day' which is, at this time 2021-04-05 16:48:41.575584+02, and put it my query like this:
select * from raws where time > '2021-04-05 16:48:41.575584+02';
It doesn't work anymore: I get 0 results, but no errors.
So what is it that makes the second query fail?
Is it something wrong with TimescaleDB or PostgreSQL, or am I missing something with formats?
So I tried to reproduce that bug on a fresh database, and everything worked fine.
The query select * from raws where time > '2021-04-05 16:48:41.575584+02'; stops working when I import all my datas from a dump, so even though I don't know exactly what, there is a problem with that dump.
Related
First question here!
So I have a table with a row like this:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
I would like to run a query that delete all the data in my table older than 14 days.
This is my query:
DELETE FROM customers WHERE timestamp < NOW() - INTERVAL 14 DAY;
This is the error: syntax error at or near "14"
Anyone knows why this is not working and/or how could I achieve my goal??
Many thanks!!
The interval value must be quoted:
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAYS';
See the doc
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAY';
Another variation on the existing answers (possibly easier to remember):
DELETE FROM customers WHERE timestamp < NOW() - '14 days'::interval;
I need to substract 4 hours from CURRENT_TIMESTAMP in DB2, my query in SQL DEVELOPER is correct because I can see the registers I need to see, but when I do the query in Eclipse have some problem and I don't know which is. This is the query I need to do:
"SELECT yerror, COUNT(yerror) AS CantidadErrores FROM kexg.VD03154 WHERE tumod BETWEEN (CURRENT_TIMESTAMP - 4 HOUR) AND CURRENT_TIMESTAMP GROUP BY yerror;";
This query returns me an error but if I put 24 HOUR is correct. Also is correct if I put 5 DAYS for example.
I have try to subtract 04 HOUR, and it's wrong also.
Thanks in advance
The following works beautifully for me on DB2 for z/OS:
select current timestamp as rightnow,
current timestamp - 4 hours
from sysibm.sysdummy1
;
RIGHTNOW COL1
-------------------------- --------------------------
2020-02-27-08.16.55.142456 2020-02-27-04.16.55.142456
I have Oracle 12c DB table and one of it's column utc_timestamp is of type
UTC_TIMESTAMP TIMESTAMP(6) WITH TIME ZONE
It stores timestamp in UTC while current_timestamp and systimestamp both gives timestamp in different timezones.
How can I get time difference in MAX(utc_timestamp) and current_timestamp in minutes ignoring time difference due to different time zones.
For example:
select current_timestamp from dual;
Gives=> 23-AUG-17 04.43.16.253931000 PM AMERICA/CHICAGO
select systimestamp from dual;
Gives=> 23-AUG-17 05.43.16.253925000 PM -04:00
select max(UTC_TIMESTAMP) from table_name;
Gives=> 23-AUG-17 09.40.02.000000000 PM +00:00
For above condition when I run SQL to check time difference between in MAX(utc_timestamp) and current_timestamp I should get number 3.
I think I need something like:
select (extract(minute from current_timestamp) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
But different timezones are messing it up and I get negative number like -4317. This might be correct as current_timestamp will be higher than max(utc_timestamp) being in CST. So I tried:
select (extract(minute from CAST(current_timestamp as TIMESTAMP(6) WITH TIME ZONE)) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
This SQL runs without error but producing a big negative number like -83461. Please help me find what am I doing wrong.
You really have two problems here.
One is to convert CURRENT_TIMESTAMP to UTC. That is trivial:
select CURRENT_TIMESTAMP AT TIME ZONE 'UTC' from dual [.....]
(use the AT TIME ZONE clause https://docs.oracle.com/cd/B19306_01/server.102/b14225/ch4datetime.htm#i1007699)
The other is that the difference between two timestamps is an interval, not a number.
select current_timestamp at time zone 'UTC'
- to_timestamp_tz('24-AUG-17 04.00.00.000 AM UTC', 'dd-MON-yy hh.mi.ss.ff AM TZR')
from dual;
produces something like
+00 00:02:39.366000
which means + (positive difference) 00 days, 00 hours, 02 minutes, 39.366 seconds.
If you just want the minutes (always rounded down), you may wrap this whole expression within extract( minute from < ...... > ). Be aware though that the answer will still be 2 (minutes) even if the difference is five hours and two minutes. It is probably best to leave the result in interval data type, unless you are 100% sure (or more) that the result is always less than 1 hour.
I'm having trouble with using the between operator in where clause. I have the following queries:
SELECT *
FROM table
WHERE timestamp_column BETWEEN (current_date - interval '1 day')::date
AND current_date => 500k rows
SELECT *
FROM table
WHERE timestamp_column >= (current_date - interval '1 day')::date => 1 mil rows
Does anyone have any idea why the result set is different? Shouldn't it be the same? I am trying to compare the number of rows from a database in PostgreSQL with data from Sybase ASE. The same 2 queries ran in Sybase give the same results. The expected result set is 1 milion rows. This should be the number of rows that I have between yesterday and today.
PostgreSQL v9.5, timestamp_column = timestamp without time zone
I tried using now(), current_timestamp but same result.
What am I missing here? If I didn't made myself clear let me know.
I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:
WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))
Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours.
Can any of you help?
WHERE workorder.createdtime > current_date - 1 -- Yesterday and today
WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr
> TIMESTAMP 'yesterday'
For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:
yesterday
today
tomorrow
now
Try SELECT TIMESTAMP 'now'.
For example, here is a query.
SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday'
ORDER BY when_row_created_ DESC
;
These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session’s time zone affects them and so on.
Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.
where workorder.createdtime >= now() - interval '24 hour'
WHERE workorder.createdtime::date = current_date - 1; --from yesterday