How to subtract 30 days from sysdate and exclude hours - sql

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>

Related

How in Oracle to calculate difference between current date and column date?

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

Apex Report to load last 30 min of data

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;

Only want report to load last 30 min of data

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!

What does Oracle SYSDATE - 1/8 mean?

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.

Trying to convert datetime in date in oracle

Hi i'm trying to convert date 01-03-2020 10:48:27 which obtained from query
SELECT
LAST_DAY( ADD_MONTHS(SYSDATE,-3 ) )+1
FROM
dual;
into '01-Mar-2020' but not able to do trying many concept
eg.
trunc(SELECT LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM dual),'YEAR')
and
SELECT TRUNC(TO_DATE('SELECT LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM dual','DD-MON-YY'), 'YEAR') "New Year" FROM DUAL;
but getting error
Any idea would be appreciated
You're making things way too complicated. Oracle TRUNC takes an additional parameter to specify whatever time interval to truncate to:
SELECT TRUNC(some_date_here, 'MON') FROM dual
If you put some_date_here as sysdate, then currently it will return 01-May-2020 until next month when it starts returning 01-Jun-2020
You can truncate to any interval; TRUNC 01/01/2000 12:34:56 with 'MI' will return 01/01/2000 12:34:00. Truncating to DD is the default (cut the time off). Truncating to DAY sets the date back to the day that started the week in the country oracle thinks it is in (probably a Sunday or Monday)
More info: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
As I understood your problem you want to go from the current date, to the first of the month that was between 2 and < 3 months ago (so if it's May now, you want to go back to first of March until it's June, when you want to go back to first of April)
If you hence, in the current date of 5th May, want to go back to a date of 1 March, take 2 months off the current date and then TRUNC to the start of the month:
SELECT TRUNC(ADD_MONTHS(sysdate, -2), 'MON') FROM dual
Don't forget you can TRUNC to the nearest quarter of a year, so if you're doing a report that is "the current quarter", then looking at a variation of TRUNC(sysdate, 'Q') would be the way to go
Lastly, I'd urge you NOT to use oracle to convert your dates to strings (in most cases) - if you keep it as a date all the way 'tIl it hits the user's computer it can be formatted for their regional preferences. If you make a decision as to the format as its coming out the dB it makes it much harder to deliver a good international experience for your app
"Convert" in your case means TO_CHAR; alter session is here to set default format for this session.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select
2 last_Day(add_months(sysdate, -3)) + 1 orig,
3 to_char(last_day(add_months(sysdate, -3)) + 1, 'dd-Mon-yyyy', 'nls_Date_language = english') result
4 from dual;
ORIG RESULT
------------------- --------------------
01.03.2020 07:25:44 01-Mar-2020
SQL>
Or, if you altered the session, you'd get it as
SQL> alter session set nls_date_language = 'english';
Session altered.
SQL> alter session set nls_date_format = 'dd-Mon-yyyy';
Session altered.
SQL> select
2 last_Day(add_months(sysdate, -3)) + 1 orig
3 from dual;
ORIG
-----------
01-Mar-2020
SQL>
But, yes - usually we TO_CHAR it.