Oracle query to get Data from table inserted in last 10 mins - oracle

I have to get data from Oracle Table in which I have one datefield called lastupdatedDate and I want to get only that rows back in which lastupdatedDate is in last 10 mins of sysdate
For example, if in my table I have lastupdateDate as 05/20/09 4:20:44 then I want this row back in my result if I run the query in between 05/20/09 4:20:44 and 05/20/09 4:30:44, and not if if I run the query at 05/20/09 5:31:44.

Or slightly more readable:
select *
from mytable
where lastupdatedDate > sysdate - interval '10' minute

select *
from mytable
where lastupdatedDate > sysdate - interval '10' minute

select sysdate - 10/(24*60) from dual;
See above example to get sysdate minus ten minutes, now just add to your query

Related

select record by date is now in oracle

I have following table
Item
Insert_Date
A
11-JAN-23
B
10-JAN-23
And I want to select records have Insert_Date equal Now date without write
select * from *My_Table* where insert_date = '11-JAN-23' ;
I tried
select * from *My_Table* where insert_date = TRUNC(CURRENT_DATE) ;
But it doesn't work;
In Oracle, a DATE is a binary data-type that ALWAYS has the components year, month, day, hour, minute and second. However, client applications (SQL*Plus, SQL Developer, etc.) often do not display the entire DATE and only display the date component and not the time component; that does not mean that the time component does not exist, only that you aren't seeing it with the default formatting.
This means that your date probably also has a non-midnight time component and your query is not matching on the time components. To solve it, you can select on a range:
SELECT *
FROM My_Table
WHERE insert_date >= TRUNC(CURRENT_DATE)
AND insert_date < TRUNC(CURRENT_DATE) + INTERVAL '1' DAY;
Or you can use TRUNC, but that would prevent you using an index on the insert_date column:
SELECT *
FROM My_Table
WHERE TRUNC(insert_date) = TRUNC(CURRENT_DATE);
Note: To change how SQL*Plus and SQL Developer format dates in your current session, you can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Try:
SELECT * FROM My_Table WHERE Insert_Date = CAST( GET_DATE() AS Date)

Finding the age of an event in Oracle

I have a table of sales with an ordered column as a timestamp type.
I would like to find the number of days since the last order. I though it should be simple.
I have tried various methods, but I can’t get a meaningful answer:
select max(ordered) from sales; -- 2022-05-17 22:47:24.467000
select sysdate-max(ordered) from sales; -- Unknown column type: 10
select current_time_stamp-max(ordered) from sales; -- Unknown column type: 10
I want to use the result in a CTE to then add to some other dates, so I thought it should at least result in either an interval type or a number of days.
How can I get the age of the above date?
There are 2 common options:
cast timestamp to date and use sysdate - cast(max(...) as date) - in this case you'll get a number in days:
SQL> select sysdate - cast(timestamp'2000-01-01 00:00:00' as date) diff1 from dual;
DIFF1
----------
8290.97766
use systimestamp - max(...) - in this case you'll get an Interval Day to Second:
SQL> select systimestamp - timestamp'2000-01-01 00:00:00' from dual;
SYSTIMESTAMP-TIMESTAMP'2000-01-0100:00:00'
------------------------------------------
+000008291 00:27:19.105859000

Oracle Max Timetamp- subtract 45 minutes

I would like to build a query, which returns all records 45 Minutes before the max timestamp.
For example the record with the latest timestamp is:
01.09.2013 11:00:00
Now I would like to have all records from
01.09.2013 10:15:00 to 11:00:00
You can accomplish this by using Max aggregate/analytic function and interval statement:
Here is an example:
select col
from ( select col
, max(col) over() as max_time
from t1) t
where t.col between t.max_time - interval '45' minute
and t.max_time
Result:
Col
--------------------
01.09.13 11:00:00 AM
01.09.13 10:45:00 AM
01.09.13 10:30:00 AM
01.09.13 10:15:00 AM
SQLFiddle Demo
with cte as
(select max(the_timestamp_field) the_timestamp_field
from test)
select
*
from
test,cte
where
test.the_timestamp_field between cte.the_timestamp_field - (1/24*.75)
and cte.the_timestamp_field
will do it.
It can be simple and a little complicated, depending on how do you want to determine the creation time of the record. If there is a date column in your table which indicates the creation time it's simple:
SELECT columns
FROM table
WHERE date_column BETWEEN TO_DATE ('01.09.2013 10:15', 'MM.DD.YYYY HH24:MI') AND
TO_DATE('01.09.2013 11:00', 'MM.DD.YYYY HH24:MI')
If there is no date column you can use SCN_TO_TIMESTAMP(ORA_ROWSCN) pseudo column to determine the creation time, but please note that this feture requires 10g or greater version.

Update Sysdate - 10 min in Oracle

I need to update a date column, with sysdate but 10 mins past. Is there any way to do?
Please help.
Like this probably
select sysdate - 10/(24*60) from dual;
UPDATE the_table
SET the_column = current_timestamp - interval '10' minute
WHERE pk = 42;
It certainly works, see here: http://sqlfiddle.com/#!4/a1986/2

Fetching column values based on SYSDATE

I have a table wchih has 2 columns. The definition is
CREATE TABLE LOGGING_T
(
TSTAMP DATE,
LINE VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/
The colulmn TSTAMP has values like 30-NOV-11, 29-NOV-11 ... and so on. Now i am doing this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 1)
The current system date is 01-DEC-11. Ideally, the above statement should return records which has TSTAMP = 30-NOV-11 since i am doing SYSDATE-1 which would be 30-NOV-11. But it isn't fetching those records. Why?
However, if i do this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 2)
Then it fetches records who TSTAMP is 30-NOV-11. Am i doing something wrong in this simple date operation?
A DATE contains time of day as well as the date.
If SYSDATE was 2011-12-01 1:18:00 PM then SYSDATE-1 would be 2011-11-30 1:18:00 PM.
Are the rows you are expecting to find from November 30th before or after the time element?
If you don't care about the time, and only want to filter based on the date, you can use TRUNC():
select *
from LOGGING_T
where TRUNC(TSTAMP) >= TRUNC(SYSDATE - 1);
You'll may or may not want to make sure both sides of your comparison operator are TRUNC()ed because TRUNC() will just force the time element of the date to be midnight.
select to_char(trunc(sysdate), 'YYYY-MM-DD HH:MI:SS PM')
from dual;
NOW
----------------------
2011-12-01 12:00:00 AM
The value SYSDATE has the time component as well. Most probably the date in your database also has the time component.
Change your query to :
select * from LOGGING_T where TSTAMP >= TRUNC(SYSDATE - 1)
to see all records which were logged from 00:00 yesterday.
To see the actual timecomponents, use to char.
SQL> select sysdate from dual;
SYSDATE
---------
01-DEC-11
1* select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS') date1 from dual
SQL> /
DATE1
--------------------
01-Dec-2011 16:29:01

Resources