I am trying to have an APEX report show only responses submitted in the last 30 minutes. Is it possible to do this with the where clause? Getting stuck with all of this errors shown below:
Here is my current code:
select SCRAP_DATE,
PAINT_SHOP,
MODEL,
COLOR,
SCRAP_OWNER,
DEPARTMENT_CODE,
REASON_FOR_SCRAP,
SCRAP_TYPE,
BODY_TYPE,
DETAILS,
COMMENTS,
SUBMITTED_DATETIME
from SCRAP_BODY_SYSTEM
SQL> select sysdate,
2 sysdate - interval '30' minute half_an_hour_ago
3 from dual;
SYSDATE HALF_AN_HOUR_AGO
---------------- ----------------
04.05.2022 14:37 04.05.2022 14:07
SQL>
SQL> select sysdate, current_timestamp from dual;
SYSDATE CURRENT_TIMESTAMP
------------------- -------------------------------
05.05.2022 07:48:19 05.05.22 07:48:19,200858 +02:00
SQL>
where submitted_datetime >= sysdate - interval '30' minute
You really shouldn't copy/paste my SQL*Plus session (from your previous question) into Apex (including the SQL> prompt) and expect it to work.
Code that might work is
select SCRAP_DATE,
PAINT_SHOP,
MODEL,
COLOR,
SCRAP_OWNER,
DEPARTMENT_CODE,
REASON_FOR_SCRAP,
SCRAP_TYPE,
BODY_TYPE,
DETAILS,
COMMENTS,
SUBMITTED_DATETIME
from SCRAP_BODY_SYSTEM
where submitted_datetime >= sysdate - interval '30' minute;
Related
I have column INACTIVE_TIME where I need to put integer number (how many days pass from some date), to represent difference between current date and column date ("LOAD_DATE" column).
In column LOAD_DATE I have data in format 03-AUG-22 03.55.57.587481000 PM.
I understand I need to get current date and than minus date from LOAD_DATE column.
I try something like this:
SELECT COUNT(*)
FROM TABLE_NAME
WHERE ((TO_DATE(SYSDATE,'DD/MM/YYYY')-(TO_DATE(LOAD_DATE,'DD/MM/YYYY'));
It is about load_date column's datatype, not the way you see that value (because it can be changed). I presume (and hope) it is timestamp; you aren't storing it as a string, are you?
If so, then you don't apply to_date to sysdate - it is a function that already returns date datatype.
Setting timestamp and date format (just to know what is what; your tool displays different format, with month name and two-digits year) (you don't have to do that).
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff9';
Session altered.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Sample table; note datatype:
SQL> create table table_name (load_date timestamp);
Table created.
SQL> insert into table_name values (systimestamp);
1 row created.
Query you're looking for (at least, I think so):
SQL> select load_date, sysdate,
2 --
3 sysdate - load_date as diff
4 from table_name;
LOAD_DATE SYSDATE DIFF
------------------------------ ------------------- ------------------------------
04.08.2022 10:22:58.101062000 04.08.2022 10:23:08 +000000000 00:00:09.898938
SQL>
To extract days, hours, minutes ... whatever, you can use that function - extract. For example:
SQL> select load_date,
2 sysdate,
3 sysdate - load_date as diff,
4 --
5 extract (day from sysdate - load_date) as diff_days,
6 extract (hour from sysdate - load_date) as diff_hours,
7 extract (minute from sysdate - load_date) as diff_minutes
8 from table_name;
LOAD_DATE SYSDATE DIFF DIFF_DAYS DIFF_HOURS DIFF_MINUTES
------------------------- ------------------- -------------------------- ---------- ---------- ------------
04.08.22 10:22:58,101062 04.08.2022 11:51:32 +000000000 01:28:33.898938 0 1 28
SQL>
Your Where clause isn't saying anything. What are you wanting it to filter?
Try
Where (sysdate - table_name.load_date) > 0
This might not be what you want, but you need to tell the query something else
I am trying to have an APEX report show only responses submitted in the last 30 minutes. Is it possible to do this with the where clause?
I originally tried using a trigger that feed into a creation date column but I had issues with data showing up into the column and was unable to have the report only show the past 30 minutes.
Any ideas would be helpful,
Thanks!
select SCRAP_DATE,
PAINT_SHOP,
IGEF,
GIRDER,
MODEL,
COLOR,
SCRAP_OWNER,
sysdate
from SCRAP_BODY_SYSTEM
where sysdate >= (current_timestamp - interval '30' minute)
where clause is the way to do it, e.g.
where date_column >= sysdate - interval '30' minute
because of this:
SQL> select sysdate,
2 sysdate - interval '30' minute half_an_hour_ago
3 from dual;
SYSDATE HALF_AN_HOUR_AGO
---------------- ----------------
04.05.2022 14:37 04.05.2022 14:07
SQL>
sysdate vs. current_timestamp:
SQL> select sysdate, current_timestamp from dual;
SYSDATE CURRENT_TIMESTAMP
------------------- -------------------------------
05.05.2022 07:48:19 05.05.22 07:48:19,200858 +02:00
SQL>
I believe you could use a where condition which would be something like:
where submitted_datetime >= (current_timestamp - interval '30' minute)
Do let me know if it works!
I am looking at a query:
select JOB_ID from db where last_updated_date >= sysdate - 1/8
I went through the ORACLE SYSDATE documentation but couldn't understand what sysdate - 1/8 means.
Please clarify.
In Oracle, it means you're subtracting 1/8 of the whole day. As one day has 24 hours, its 1/8th part is 24/8 = 3 hours. So:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate col1,
2 sysdate - 1/8 col2
3 from dual;
COL1 COL2
------------------- -------------------
29.10.2021 22:18:36 29.10.2021 19:18:36
SQL>
date value remained the same (it is still today, 29.10.2021)
time has changed; right now, it is 22:18:36. When we subtract 3 hours from it, we get 19:18:36 (3 hours earlier)
It means that your query fetches rows whose last_updated_date column value is within the last 3 hours.
It will subtract 3 hours (24 hours/8) from sysdate, try running the following:
select to_char((sysdate - 1/8), 'mm/dd/yyyy hh24:mi:ss') from dual
This will give you 3 hours previous to the current date/time of the database.
i am trying to subtract 30 days from sysdate
The query that i am using is (sysdate - 30). However, this includes the current sysdate hours and minute.
i want it to include all data where sysdate is -30 without specificing the hour and minute.
For example:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy hh24:Mi:ss';
Session altered.
SQL> select sysdate,
2 sysdate - 30 thirty_days_ago,
3 trunc(sysdate - 30) truncated
4 from dual;
SYSDATE THIRTY_DAYS_AGO TRUNCATED
------------------- ------------------- -------------------
26.09.2019 11:10:18 27.08.2019 11:10:18 27.08.2019 00:00:00
SQL>
If you want to display it without time component, either alter session again (but using different data format, e.g. dd.mm.yyyy), or apply TO_CHAR function:
SQL> select to_char(trunc(sysdate - 30), 'dd.mm.yyyy') without_time
2 from dual;
WITHOUT_TI
----------
27.08.2019
SQL>
I need to generate a list of values in an Oracle DB with the following columns of data:
ITEM_TYPE VARCHAR2(20)
ITEM_LAST_UPDATED DATE
ITEM_UPDATE_TOLERANCE NUMBER(1)
The only data that should be send out to the console would be items that have the date in 'ITEM_LAST_UPDATED' less than the sysdate minus the integer value within 'ITEM_UPDATE_TOLERANCE'.
So, if I wanted to just show the ones that were one hour past due, I can do:
select ITEM_TYPE from MY_TABLE
where
to_char(ITEM_LAST_UPDATED, 'DD-MON-YYYY HH24:MI')
<=
to_char(sysdate - interval '1' hour, 'DD-MON-YYYY HH24:MI');
However, rather than using the '1' in the above statement, I need to replace it with the numeric value of ITEM_UPDATE_TOLERANCE.
I tried several different versions, but all error (such as):
select ITEM_TYPE from MY_TABLE
where
to_char(ITEM_LAST_UPDATED, 'DD-MON-YYYY HH24:MI')
<=
to_char(sysdate - interval to_number(ITEM_UPDATE_TOLERANCE) hour, 'DD-MON-YYYY HH24:MI');
Why are you converting a perfect DATE column to a character value just to compare it another DATE value converted to a character column.
Simply use:
ITEM_LAST_UPDATED <= sysdate - interval '1' hour
To achieve what you want, just multiply the value:
ITEM_LAST_UPDATED <= sysdate - (interval '1' hour) * ITEM_UPDATE_TOLERANCE
There is also absolutely no need to convert a number to a number using the to_number() function.
As an alternative to #a_horse_with_no_name's interval multiplication trick, or San's division method, you can also use the numtodsinterval() function:
ITEM_LAST_UPDATED <= sysdate - numtodsinterval(ITEM_UPDATE_TOLERANCE, 'HOUR')
As an example:
select sysdate, sysdate - numtodsinterval(3, 'HOUR') from dual;
SYSDATE SYSDATE-NUMTODSINTE
------------------- -------------------
2014-03-07 19:08:27 2014-03-07 16:08:27
Well you can try using simple calculation
select ITEM_TYPE from MY_TABLE
where
ITEM_LAST_UPDATED
<=
sysdate - (ITEM_UPDATE_TOLERANCE/24);
Calculation of ITEM_UPDATE_TOLERANCE/24 will convert hours into days and then can be subtracted from sysdate.