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

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

Related

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.

Extracting the month of the date in a column [duplicate]

This question already has answers here:
Get month name from date in Oracle
(7 answers)
Closed 2 years ago.
I need help in extracting the month of the date in a certain column.
For example the whole table is called A and the column for date is called END_TIME which has a format like this MM-DD-YYYY HH24:MI:SS.
I want my output to be the name of the month.
If END_TIME column's datatype is DATE (should be, looks like it is), then you should apply TO_CHAR function to it with desired format mask. Here's an example:
SQL> create table a as select sysdate end_time from dual;
Table created.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * From a;
END_TIME
-------------------
13.08.2020 08:51:04
SQL> select to_char(end_time, 'Month', 'nls_date_language = english') mon from a;
MON
---------
August
SQL>
Thats pretty starightforward you can make use of to_char function like below.
select to_char(sysdate, 'MONTH') FROM DUAL;
Already answered here

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.

How to subtract 30 days from sysdate and exclude hours

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>

Oracle SQL What does start_date-1 mean?

Oracle SQL What does start_date-1 mean?
Is it just one day less the start_date?
Assuming your datatype is a date or timestamp, yes, it will remove a full day from your date:
SELECT SYSDATE AS "start_date",
SYSDATE - 1 AS "start_date -1"
FROM DUAL;
Returns
26/08/2015 11:09:21 | 25/08/2015 11:09:21
YES. It will subrtract 1 day from the start_date.
Given that the data type of start_date is DATE or TIMESTAMP, subtracting N from it would subtract N number of days.
For example,
SQL> alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS';
Session altered.
SQL> SELECT sysdate, sysdate -1 FROM DUAL;
SYSDATE SYSDATE-1
------------------- -------------------
26-08-2015 14:45:35 25-08-2015 14:45:35
SQL>
Remember, date has both date and time elements, so you will go back to previous day with exactly that time portion.
Yes it is 1 day earlier to start_date.