Fetching column values based on SYSDATE - oracle

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

Related

PL SQL data is in numeric format (20211023) i want to use the where clause on date column +30days

Select * from Table where date >='20210911' + 30days
the date is in numeric format and what to pull the records for a specific date +30days of specific date
Could you please help
Uh. Never store dates into any other datatype column but DATE. Now you first have to "convert" it, then do the arithmetic.
select *
from some_table
where to_date(date_column, 'yyyymmdd') > date '2021-09-11' + 30
--------
apply format mask that matches data in that column
Hope (should I say pray?) that all values represent valid dates. Nobody prevents you to store e.g. 20228579 into it, and - applying to_date to it - results in
SQL> select to_date('20228579', 'yyyymmdd') from dual;
select to_date('20228579', 'yyyymmdd') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL>
Once again, bad, BAD idea!
how to apply between clause ( where date_column between date '2021-09-11' and date '2021-09-11'+30
If you have an index on the column that you want to use then convert the value to a date then add 30 days to it and convert it back to a number:
SELECT *
FROM Table_Name
WHERE date_number BETWEEN 20210911
AND TO_NUMBER(
TO_CHAR(
TO_DATE(20210911, 'YYYYMMDD')
+ INTERVAL '30' DAY,
'YYYYMMDD'
)
)
If you don't have an index and want a simpler query then:
SELECT *
FROM Table_Name
WHERE TO_DATE(date_number, 'YYYYMMDD') BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
The best solution would be to convert your numeric column to a DATE column:
ALTER TABLE table_name ADD date_column DATE;
UPDATE table_name SET date_column = TO_DATE(date_number, 'YYYYMMDD');
ALTER TABLE table_name DROP COLUMN date_number;
Then:
SELECT *
FROM Table_Name
WHERE date_column BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
db<>fiddle here

Facing issue with date function

I am trying to get the value from dba_scheduler_job_run_details view.
where log_date > (SYSDATE - 5/(24*60)) (i.e. data of last 5 mins)
But it is not providing me with the correct value.
For sample please find the data below:
SQL Query:
SELECT SYSDATE, SYSDATE - 5/(24*60),LOG_DATE,TRUNC(LOG_DATE),TO_CHAR(LOG_DATE,'YYYY/MM/DD HH:MI:SS')
FROM dba_scheduler_job_run_details
WHERE LOG_DATE > (SYSDATE - 5/(24*60))
ORDER BY LOG_DATE DESC;
o/p:
image attached
This is because log_date is timestamp with timezone datatype and when you are joining it with date datatype it will lead to two different times when Oracle does an implicit conversion to compare:
SQL> SELECT SYSTIMESTAMP, CAST(SYSDATE AS TIMESTAMP WITH TIME ZONE) time_with_tz FROM dual;
SYSTIMESTAMP TIME_WITH_TZ
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
19-MAR-18 09.57.07.654161 AM -04:00 19-MAR-18 09.57.07.000000 AM +05:30
So if I execute you query from time zone with offset +5:30 on a server with offset -4:00, Oracle will convert the SYSDATE - 5/(24*60) to sysdate - (9:30 + 0:05).
Tell Oracle to convert log_date to date explicitly and then apply the filer of > (SYSDATE - 5/(24*60) as follows and you will be fine:
SELECT SYSDATE, SYSDATE - 5/(24*60),LOG_DATE,TRUNC(LOG_DATE),TO_CHAR(LOG_DATE,'YYYY/MM/DD HH:MI:SS')
FROM dba_scheduler_job_run_details
WHERE cast(LOG_DATE AS DATE) > (SYSDATE - 5/(24*60))
ORDER BY LOG_DATE DESC;
Update: This query may have performance issue due to following reasons,
1) Oracle will convert datatype for each row for column LOG_DATE from
timestamp with time zone to date.
2) The build in index on column LOG_DATE, if any, will not be used due
to the CAST function.
Better solution is to convert the sysdate - 5/(24*60) to timestamp with time zone, you must be aware of the timezone of your Oracle server, assuming that the server is located in UTC -4:00, below query should work fine:
SELECT SYSDATE, SYSDATE - 5/(24*60),LOG_DATE,TRUNC(LOG_DATE),TO_CHAR(LOG_DATE,'YYYY/MM/DD HH:MI:SS')
FROM dba_scheduler_job_run_details
WHERE LOG_DATE > TO_TIMESTAMP_TZ (to_char(SYSDATE - 5/(24*60), 'YYYY-MM-DD HH24:MI:SS') || ' -4:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')
ORDER BY LOG_DATE DESC;
Another easier approach as suggested by #mathguy, use INTERVAL function:
SELECT SYSDATE, SYSDATE - 5/(24*60),LOG_DATE,TRUNC(LOG_DATE),TO_CHAR(LOG_DATE,'YYYY/MM/DD HH:MI:SS')
FROM dba_scheduler_job_run_details
WHERE LOG_DATE > SYSTIMESTAMP - INTERVAL '5' MINUTE
ORDER BY LOG_DATE DESC;

Find last 1800 second(30 minutes) data in a log table

I have a table with a log_date column (dd mm-yy-hh-m-ss format)
What I have tried is:
select * from dum where(sysdate-log_date)<=1/48;
But does not worked at all so please suggest what should i use?
SELECT * FROM dum WHERE log_date BETWEEN (SYSTIMESTAMP - 1800/(24*60*60)) AND SYSTIMESTAMP;
Assuming the LOG_DATE column is of DATE data type, you can use:
SELECT *
FROM DUM
WHERE log_date >= SYSDATE - INTERVAL '30' MINUTE;
If your LOG_DATE column is a VARCHAR2 then just wrap it in TO_DATE():
SELECT *
FROM DUM
WHERE TO_DATE( log_date, 'dd mm-yy-hh-mi-ss' ) >= SYSDATE - INTERVAL '30' MINUTE;
(and then look at changing your table to use a DATE data type to store dates rather than using strings.)

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Get date of the previous day in Oracle

I need to bring the day immediately preceding date in Oracle using a truncate but not how. He was using the following line but bring me some records for the current day of execution and should not be. Neceisto only the previous day; investigation found the truncate with dates in Oracle but not how to use it.
and fnxs.FECHA_INGRESO BETWEEN (TO_CHAR (SYSDATE-1, 'DD-MON-YY')) AND (TO_CHAR (SYSDATE, 'DD-MON-YY'));
I appreciate your help
Using BETWEEN with dates in Oracle is generally a bad idea. I see it all the time, and most of the time people get it wrong (like in the accepted answer above). Even when they fully understand that the two dates are included, they still make logical errors because they forget about timestamps.
The OP is asking for yesterday dates. The following sql shows that today falls within "BETWEEN TRUNC( SYSDATE ) - 1 AND TRUNC( SYSDATE )"
with adate as (
select trunc(sysdate) today from dual
) select today from adate where today between trunc(sysdate) -1
and trunc(sysdate);
16-Apr-15 00:00:00
[returns the record for today]
I find it easier to be correct with dates when you're more explicit about the end points:
SELECT * from your_table
WHERE fnxs.FECHA_INGRESO >= TRUMC(SYSDATE) - 1
AND fnxs.FECHA_INGRESO < TRUNC(SYSDATE);
Upon looking closer, the OP's date-like column might be a VARCHAR2 (could still be a date that was implicitly cast in the comparison he gave). If it is a VARCHAR, then it needs to be converted first (using an appropriate format string):
SELECT * FROM your_table
WHERE TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') >= TRUMC(SYSDATE) - 1
AND TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') < TRUNC(SYSDATE);
Assuming your column is of type DATE
SELECT *
FROM TABLE_NAME
WHERE FECHA_INGRESO BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );
If it is a character string then:
SELECT *
FROM TABLE_NAME
WHERE TO_DATE( FECHA_INGRESO, 'DD-MON-YY' )
BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );

Resources