SYSDATE - Timestamp to retrieve orders from last 2 minutes - sql

This is used in a database package as a delay to pass data. The delay is 2 minutes so I need to only retrieve records if they've been in the database for 2 minutes or more.
This is what I have:
((SYSDATE - trunc(last_updated))*24*60) > l_delay_mins;
l_delay_mins = 2 minutes in this case.
However, the trunc cuts off the time and defaults to midnight, when the conversion is done it gives me the fraction of the day and then multiplies 24*60 which is always larger than 2, so the records aren't being delayed and are sending as soon as they arrive.

Use INTERVAL:
where last_updated < systimestamp - (l_delay_mins * interval '1' minute);
Here is the same thing without INTERVAL but with a minute being a fraction of the day (kind of what you tried):
where last_updated < systimestamp - (l_delay_mins / 24 / 60)

A minor amendment to the above answer
Use INTERVAL:
where last_updated <= systimestamp - (l_delay_mins * interval '1' minute);

Related

PostgresSQL - the date works weird

I need create select for all rows it are older than 15 minutes at the moment.
SELECT last_answer_date FROM messages WHERE last_answer_date > NOW() - INTERVAL '15 minutes';
When I start this select now in 16:51:00 I get result:
2019-01-17 16:25:00
In other words I mean:
Give me all rows where last_answer_date > 16:51:00 - 15 minutes (because now 16:51:00)
Give me all rows `where last_answer_date > 16:36:00 (because now 16:51:00 - 15 minutes = 16:36:00)
And I get 16:25:00 but 16:25:00 < 16:36:00
EDIT: When I change > to < I get 2019-01-16 17:50:27
EDIT2: Example:
SELECT insert_date FROM smev_messages WHERE insert_date < NOW() - INTERVAL '15 minutes';
now = 17:16
change to SELECT insert_date FROM smev_messages WHERE insert_date > NOW() - INTERVAL '15 minutes';
17:17
I think it might be a problem with your timezone.
You can check it with show timezone;
Or simply go with select now() and see if it matches your expectations.
If you have a big table with a lot of data you will get some performance problems because when you call
NOW() - INTERVAL '15 minutes' internally postgress need convert your interval to a date
Postgress Interval
I think a better way to-do that you should do something like that to improve performance, because all dates are long values and you should compare it like long values.
-- 15 minutes in millis = 900000
-- 15 minutes in secods = 900
-- Retrive all data inserted at latest 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) < 900;
-- Retrive all data inserted more than 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) > 900;

How to get data from the last 5 FULL minutes?

If I write:
SELECT *
FROM table
WHERE date >= NOW - INTERVAL '5 MINTUE';
I will get all the data from the last 5 minutes, but the first and last minute won't be full minutes, they'll likely be partial minutes. Meaning if "NOW()" is 4:30:24, then NOW() - INTERVAL '5 MINUTE' will get me from 4:25:24 to 4:30:24. Minute 25 is partial and only contains 36 seconds, same with minute 30 which only contains 24 seconds.
How would I go about getting the last full 5 minutes? I considered removing the first and last minute or min/max, but that idea became a challenge when I consider minute [58, 59, 60, 01, 02]. In this case min/max doesn't account for the cycle back to 0 after minute 60.
Any ideas would be helpful.
You can use date_trunc():
SELECT *
FROM table
WHERE date >= date_trunc('minute', now()) - interval '5 minute' and
date < date_trunc('minute', now())
Try something like this
SELECT *
FROM table
WHERE date >= DateADD(mi, -5, Current_TimeStamp)

Query to fetch records only form previous half hour with time stamp in unix epoch format

I want SQL query to fetch/select records which are taken only from previous half an hour only. For example if my scheduler ran at 2 pm, and then again in 2:30, during the 2:30 run it should only pick rows from between 2pm and 2:30pm and not earlier, using the column created_timestamp which stores the time as unix epoch format eg:
|created_timestamp|
|1497355750350 |
|1497506182344 |
We can do arithmetic with Oracle dates. Subtracting one date from another gives the interval as a fractional number. Multiplying by 86400 gives us the number of seconds. So this is the current unix expoch:
(sysdate - date '1970-01-01') * 86400
This means your query will be something like
select * from your_table
where created_timestamp >= (:last_run_time - date '1970-01-01') * 86400
The trick is that your scheduler needs to pass in the time of the previous run - last_run_time - to pick up all the records which have been added since then.
You can do Flashback query
SELECT * FROM TABLE
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '30' MINUTE);

How to convert a single Oracle datetime into minutes?

I wish to convert a single Oracle datetime value to minutes.
As an example, I need to convert the current date/time alone into minutes, i.e.:
select (sysdate)*24*60 from dual
but this is giving me an error.
I basically need to perform a check to see that a certain operation cannot be performed until 30 minutes before a particular date/start time, that exists in the database.
So if the Start Time in the DB is:
24/04/2014 22:00:00 and the current date/time (SYSDATE) is 24/04/2014 21:29:59,
then operation CANNOT be performed but if the current date/time (SYSDATE) is:
24/04/2014 21:30:00,
then operation CAN be performed.
You probably want something like
startTime - interval '30' minute >= sysdate
or
startTime >= sysdate + interval '30' minute
You could also subtract the two date values which gives you the number of days between them and multiply
(startTime - sysdate)*24*60 >= 30
but I generally find the interval notation clearer and easier to read. It's also easier to structure in a way that allows you to use indexes on columns like startTime.
select (sysdate - trunc(sysdate)) *24 *60 from dual
You seem to want to know if the seconds component of sysdate is 0. So, test for that:
where extract(second from sysdate) = 0
Oops, I misread the question. You just need a difference of 30 minutes. That is also easy:
where starttime <= sysdate + 30/(24*60)
When you add an integer to a datetime, it is interpreted as a number of days. The expression 30/(24*60) is an expression for half an hour measured in days.

Oracle: subtract millisecond from a datetime

I thought it was really simple but it isn't.
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY')
- 1/(24*50*60*1000) data
FROM dual;
It simply doesn't work.
Other details:
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY') -
NUMTODSINTERVAL(1/(24*50*60*1000),'HOUR') data
FROM dual;
doesn't work..
The right seems to be
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY') -
NUMTODSINTERVAL(1/(24*25*60*1000),'HOUR') data
FROM dual;
Why? How does it work?
For adding or subtracting an amount of time expressed as a literal you can use INTERVAL.
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY')
- INTERVAL '0.001' SECOND
FROM dual;
As well there are now standard ways to express date and time literals and avoid the use of various database specific conversion functions.
SELECT TIMESTAMP '2012-10-08 00:00:00'
- INTERVAL '0.001' SECOND DATA
FROM dual;
For your original question the time part of a day is stored in fractional days. So one second is:
1 / (hours in day * minutes in hour * seconds in a minute)
Divide by 1000 to get milliseconds.
1 / (24 * 60 * 60 * 1000)
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY') - NUMTODSINTERVAL(1/(24*50*60*1000),'HOUR') data
FROM dual;
OUTPUT
DATA
---------------------------------
09/AUG/12 11:59:59.999950000 PM
1 row selected.
The answer posted above subtracts a tenth of a millisecond from the date. I think what you want is the following:
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY')-NUMTODSINTERVAL(1/1000,'SECOND')
FROM dual;
Output:
DATA
---------------------------------------------------------------------------
09-AUG-12 11.59.59.999000000 PM
^^^
|||
tenths|thousandths
|
hundredths
The following NUMTODSINTERVAL(1/(24*25*60*1000),'HOUR') seems to work only because 24*25 = 600. But that number is wrong because 1/(600*60*1000) of an hour is a tenth of a millisecond, not a millisecond. If you want to use 'HOUR' in NUMTODSINTERVAL() you should use 1/(60*60*1000) (sixty minutes in an hour, sixty seconds in a minute, 1000 ms in a second).
This is correct (with a millisecond being 1000th of a second):-
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY') - NUMTODSINTERVAL(1/1000,'SECOND') data FROM dual;
DATA
-----------------------------
09-AUG-12 23.59.59.999000000
As to why the other code isn't working it's because you aren't calculating a millisecond correctly. An hour must be divided by 60 to give minutes and again by 60 to given seconds then by 1000 to give a millisecond, thus if you must use HOUR as the interval then it is:-
SELECT TO_TIMESTAMP('10/08/2012','DD/MM/YYYY') - NUMTODSINTERVAL(1/(60*60*1000),'HOUR') as data FROM dual;
DATA
---------------------------------------------------------------------------
09-AUG-12 23.59.59.999000000
select TO_CHAR(TO_TIMESTAMP('10.05.2012', 'DD.MM.YYYY') -
NUMTODSINTERVAL(1/1000, 'SECOND'), 'DD.MM.YYYY HH24:MI:SS:FF3') Res
from dual;
RES
-----------------------------
09.05.2012 23:59:59.999