Oracle query not giving result for current_date - oracle

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()

Related

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;

Oracle comparing timestamp with date

I have a timestamp field and I just want to compare the date part of it in my query in Oracle
How do I do that,
SELECT *
FROM Table1
WHERE date(field1) = '2012-01-01'
You can truncate the date part:
select * from table1 where trunc(field1) = to_date('2012-01-01', 'YYYY-MM-DD')
The trouble with this approach is that any index on field1 wouldn't be used due to the function call.
Alternatively (and more index friendly)
select * from table1
where field1 >= to_timestamp('2012-01-01', 'YYYY-MM-DD')
and field1 < to_timestamp('2012-01-02', 'YYYY-MM-DD')
You can truncate the date
SELECT *
FROM Table1
WHERE trunc(field1) = to_Date('2012-01-01','YYY-MM-DD')
Look at the SQL Fiddle for more examples.
to_date format worked for me. Please consider the date formats:
MON-, MM, ., -.
t.start_date >= to_date('14.11.2016 04:01:39', 'DD.MM.YYYY HH24:MI:SS')
t.start_date <=to_date('14.11.2016 04:10:07', 'DD.MM.YYYY HH24:MI:SS')

using update in oracle

I have a column named date_col of data-type date. What's wrong with this query?
update test set date_col = to_date(sysdate,'DD-Mon-YYYY HH24:MI:SS')
Only date mon and yy is visible. not the time.
How can I make it work?
SYSDATE is yet a date. You don't need to cast SYSDATE to date type because it is a date_
update test
set date_col = sysdate
To see time fraction use to_char:
select to_char(date_col, 'HH24 MI')
from test;

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

Date function on oracle

I've got a question about date function on oracle.
I have the following table
statistic_table(
pages AS varchar(10),
date_created AS date
);
I have the following sql
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN sysdate-5 AND sysdate-1
and
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN to_date('12-AUG-2011') AND to_date('16-AUG-2011');
the question is, why is it return different numbers. assuming sysdate-5 returns 12-aug-2011 and sysdate-1 returns 16-aug-2011
Any help would be much appreciated!
Cheers,
sysdate - 5 will give you a date with the current time. So if I ran it at 1pm precisely, the query would be equivalent to:
select (*)
FROM statistic_table
WHERE date_created BETWEEN to_date('12-Aug-2011 13:00:00')
AND to_date('16-Aug-2011 13:00:00')
whereas the second query is:
select (*)
FROM statistic_table
WHERE date_created BETWEEN to_date('12-Aug-2011 00:00:00')
AND to_date('16-Aug-2011 00:00:00')
you should probably try this instead:
select (*)
FROM statistic_table
WHERE date_created BETWEEN trunc(sysdate) -5
AND trunc(sysdate) -1
A date in Oracle is a point in time with a precision of a second.
SYSDATE returns the current date and time and is therefore not the same as to_date('17-AUG-2011'):
SQL> SELECT to_char(sysdate, 'dd-mon-yyyy hh24:mi:ss') FROM DUAL;
TO_CHAR(SYSDATE,'DD-MON-YYYYHH
------------------------------
17-aug-2011 15:52:13
Use the TRUNC function if you only want the date component:
SQL> SELECT to_char(trunc(sysdate), 'dd-mon-yyyy hh24:mi:ss') FROM DUAL;
TO_CHAR(TRUNC(SYSDATE),'DD-MON
------------------------------
17-aou-2011 00:00:00
Because SYSDATE includes a time component, so if the current time is 11:22:33, then
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN sysdate-5 AND sysdate-1
is actually equivalent to
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN to_date('12-AUG-2011 11:22:33','DD-MON-YYYY HH24:MI:SS')
AND to_date('16-AUG-2011 11:22:33','DD-MON-YYYY HH24:MI:SS')
To avoid the time component do this:
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN TRUNC(sysdate)-5 AND TRUNC(sysdate)-1
An Oracle DATE always has a day and a time component.
sysdate-5 returns a date exactly 5 days ago. If today is August 17 at 10 AM, for example, sysdate-5 returns August 12 at 10 AM.
to_date('12-AUG-2011', 'DD-MON-YYYY'), on the other hand, returns August 12 at midnight. So it returns a date that is 10 hours earlier than sysdate-5.
sysdate auto returns with a time component as mentioned by the previous answers.
When using to_date it is converting a string to a date. With this being said you can pass in parameters to make it return the same thing.
Have a look at this link that explains it.
to_date parameters

Resources