I have a query that I am trying to use TO_DATE to check if ERROR_DT is a data that is within one hour of the current time
Here is what I have so far
SELECT BERROR_DT FROM SomeTable
WHERE ERROR_DT>=TO_CHAR(TO_DATE( SYSDATE, 'MM/DD/YYYY HH12:MI:SS AM') -1, 'fmMM/DDfm/YYYY HH12:MI:SS AM');
Error_DT has a value of (e.g.) 5/18/2020 6:45:15 PM
When I run this I get
ORA-01830: date format picture ends before converting entire input string
I followed the said link and it still is not working. How would I fix this so that I can still remove all 0s in front of the month and the date?
I would suggest converting the date string to the corresponding date value, then do the comparison:
select berror_dt
from sometable
where to_date(error_dt, 'fmMM/DD/YYYY HH12:MI:SS AM') >= sysdate - interval '1' hour
Bottom line, you should fix your data model and store dates as a date-like datatype rather than as a string. The above predicate is not efficient, because the conversion needs to be executed for each and every value of error_dt before the filtering applies, hence defeating an existing index on the column.
Obviously wrong thing you're doing is applying TO_DATE to SYSDATE which is a function that returns DATE datatype.
What you could do is to subtract sysdate and error_dt (I presume its datatype is DATE as well) and see whether difference is less than 1 hour. As difference of two dates is number of days, you have to divide it by 24 (as there are 24 hours in a day).
Something like this:
SQL> alter session set nls_date_format = ' dd.mm.yyyy hh:mi:ss am';
Session altered.
SQL> with test (id, error_dt) as
2 (select 1, to_date('18.05.2020 10:30:15 PM', 'dd.mm.yyyy hh:mi:ss am') from dual
3 union all
4 select 2, to_date('18.05.2020 05:20:55 AM', 'dd.mm.yyyy hh:mi:ss am') from dual)
5 select t.id, t.error_dt, sysdate
6 from test t
7 where sysdate - t.error_dt < 1 / 24;
ID ERROR_DT SYSDATE
---------- ----------------------- -----------------------
1 18.05.2020 10:30:15 PM 18.05.2020 11:02:24 PM
SQL>
If ERROR_DT is a DATE value you just need to use something like
SELECT BERROR_DT
FROM SomeTable
WHERE ERROR_DT >= SYSDATE - INTERVAL '1' HOUR
or if you prefer to use old-fashioned pre-INTERVAL calculations
SELECT BERROR_DT
FROM SomeTable
WHERE ERROR_DT >= SYSDATE - (1/24)
Related
I have a table which hold only time part.
TIME
08:00
08:00
08:00
08:00
18:00
19:00
20:00
Now, i need to concatenate the time with current date. for that i tried with below code.
to_date( to_char (sysdate,'YYYYMMDD')|| Time,'HH24MI')
but that is not working as expected and getting the below error
ORA-01830: date format picture ends before converting entire input string
Can you please help me to solve this problem.
Expected output(date time format):
14-Feb-23 08:00
14-Feb-23 09:00
What datatype do you expect as result? A string?
SQL> with test (time) as
2 (select '08:00' from dual)
3 select to_char(sysdate, 'dd-Mon-yy ') || time as result
4 from test;
RESULT
---------------
14-Feb-23 08:00
If you expect date, then
SQL> with test (time) as
2 (select '08:00' from dual)
3 select to_date(to_char(sysdate, 'dd-Mon-yy ') || time, 'dd-Mon-yy hh24:mi') as result
4 from test;
RESULT
--------
14.02.23
Hm, what format is that?! My default! Modify it to something else, e.g. what you said you want but - nonetheless - value previous code returns is a valid date value:
SQL> alter session set nls_date_format = 'dd-Mon-yy hh24:mi';
Session altered.
SQL> with test (time) as
2 (select '08:00' from dual)
3 select to_date(to_char(sysdate, 'dd-Mon-yy ') || time, 'dd-Mon-yy hh24:mi') as result
4 from test;
RESULT
---------------
14-Feb-23 08:00
SQL>
You have time strings like '08:00'. And you have to_date( to_char (sysdate,'YYYYMMDD')|| Time,'HH24MI').
The expression to_char (sysdate,'YYYYMMDD')|| Time results in something like '2023021408:00'.
You want to convert this result string into a datetime (that Oracle inappropriately calls DATE) with TO_DATE: to_date('2023021408:00', 'HH24MI').
You do notice, that the string you have constructed and the format you are using don't match? Your string's format is not 'HH24MI', but 'YYYYMMDDHH24:MI'.
Hence use:
TO_DATE(TO_CHAR(SYSDATE,'YYYYMMDD') || time, 'YYYYMMDDHH24:MI')
I have to calculate time difference in minutes from current(sysdate) and modified time:-
to_date(to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
- to_date(to_char(modified, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
but problem is to_char returns proper time:-
to_char(whenmodified, 'YYYY-MM-DD HH24:MI:SS')
Outputs 2016-05-23 14:55:50
and to_date doesn’t show time:-
to_date(to_char(modified, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
Outputs: 2016-05-23
Please assist how I can get time difference by converting to_char to to_date.
NOTE:
I cant do sysdate-modified because both sysdate and modified gives date without time e.g 2016-05-23
Using to_char for sysdate or modified give date with time 2016-05-23 14:55:50
As we cant subtracts dates in to_char function I am again converting back them to to_date for getting time.
I am expecting:
2016-05-23 14:55:50 - 2016-05-23 14:53:50 = 2 min
I have to calculate time difference in minutes from current(sysdate) and modified time
Oracle Setup:
CREATE TABLE table_name ( modified DATE );
INSERT INTO table_name
SELECT TIMESTAMP '2016-05-23 14:20:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-05-23 00:00:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-05-01 00:00:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-01-01 00:00:00' FROM DUAL;
Query:
SELECT ( sysdate - modified ) * 24 * 60 AS minute_difference
FROM table_name;
Output:
MINUTE_DIFFERENCE
-----------------
3.66666667
863.666667
32543.6667
206783.667
And to address your comment that:
to_date doesn’t show time
A date always has a time component and never has a format internally to the database (it is represented by 7 or 8 bytes) - the formatting of a date is done by the client program that you use to access the database (and often the default is not to show the time component - however, the time component still exists).
You can change this either in the preferences of your client program or, if they don't use that to control it, by changing the NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
The following sql when run with these parameters,
:P_COMP_DATE_FROM = '15-NOV-2015'
:P_COMP_DATE_TO = '15-NOV-2015'
compares as between '15-NOV-2015 00:00:00' and '15-NOV-2015 00:00:00'
Select Ordered_date
From xxcost_rep
Where DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(:P_COMP_DATE_TO)), DATE_COMPLETED);
how can I compare this as start of the day and end of the day, so can display the correct result in the range.
I am trying the following to add 86399 seconds to make it the end of the day, but receiving error:
WHERE DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(to_date(:P_COMP_DATE_TO,'DD-MON-YYYY')+interval '86399' second), DATE_COMPLETED)
{P_TO_CUSTOMER=, P_COMP_DATE_FROM=2015/11/15 00:00:00, P_COMP_DATE_TO=2015/11/15 00:00:00, P_TO_ORDER_NUMBER=, P_CUST_REGION=, P_TO_DATE=, P_JOB_STATUS=, P_FROM_DATE=, P_FROM_ORDER_NUMBER=, P_FROM_CUSTOMER=}
Calling XDO Data Engine...
--SQLException
java.sql.SQLDataException: ORA-01861: literal does not match format string
ORA-01861: literal does not match format string
The above error is because the date literal doesn't match with the format mask.
For example,
SQL> SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual;
SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
You might be storing dates as string, and there might be strings with different date formats. Therefore, your function fnd_date.canonical_to_date might be failing for such date literals while converting into DATE using TO_DATE.
Also, you should not depend on your client's NLS date format. Remember, TO_DATE is NLS dependent. You should explicitly mention the format mask.
For example,
SQL> SELECT to_date('11/18/2015 00:00:00', 'mm/dd/yyyy hh24:mi:ss') date_from,
2 to_date('11/18/2015 23:59:59', 'mm/dd/yyyy hh24:mi:ss') date_to
3 FROM dual;
DATE_FROM DATE_TO
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59
In your case, you need to compare the dates. You could do it like the below example,
SQL> WITH DATA AS(
2 SELECT DATE '2015-11-18' dt FROM dual
3 )
4 SELECT * FROM DATA
5 WHERE dt
6 BETWEEN to_date(
7 to_char(dt, 'mm/dd/yyyy')||' 00:00:00',
8 'mm/dd/yyyy hh24:mi:ss'
9 )
10 AND to_date(
11 to_char(dt, 'mm/dd/yyyy')||' 23:59:59',
12 'mm/dd/yyyy hh24:mi:ss'
13 );
DT
-------------------
11/18/2015 00:00:00
UPDATE
For the first part where you just need the start time, you don't have to add the time portion as 00:00:00since DATE has both date and time elements. When you do not mention the time portion, it defaults to midnight i.e. 00:00:00.
For example, add INTERVAL '86399' SECOND:
SQL> SELECT DATE '2015-11-18' from_date,
2 DATE '2015-11-18' + INTERVAL '86399' SECOND to_date
3 FROM dual;
FROM_DATE TO_DATE
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59
I would like to query number of records between night 12.01 AM to 11.59 PM but issue is, I would like to schedule this query so I cant specify any hard coded dates.
Query should pull number of records for query date between 12.01 AM to 11.59 PM.
Could someone please help me on this.
Query should pull number of records for query date between 12.01 AM to 11.59 PM.
You could do it as:
TRUNC gives you date element truncating the time portion
convert the SYSDATE into string using TO_CHAR
then concatenate the time element
finally convert everything back to DATE
SYSDATE returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE, and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter.
So, you don't have to hard-code any DATE value if you want to execute the query everyday.
Use the following in the filter predicate:
BETWEEN
TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY') ||' 00:01', 'MM/DD/YYYY HH24:MI')
AND
TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY') ||' 23:59', 'MM/DD/YYYY HH24:MI')
Demo
SQL> alter session set nls_date_format = 'MM/DD/YYYY HH24:MI:SS';
Session altered.
SQL> SELECT to_date(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
2 ||' 00:01', 'MM/DD/YYYY HH24:MI') start_dt ,
3 to_date(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
4 ||' 23:59', 'MM/DD/YYYY HH24:MI') end_date
5 FROM dual;
START_DT END_DATE
------------------- -------------------
05/06/2015 00:01:00 05/06/2015 23:59:00
SQL>
So, you don't have to put any hard-coded value for current date, the SYSDATE will take care of it. All you are doing is:
TRUNC gives the date element by truncating the time portion.
Then concatenating the required time element
Converting the entire string into DATE using TO_DATE
I would like to schedule this query so I cant specify any hardcord dates
To schedule the query to execute everyday, you could use DBMS_SCHEDULER.
I'm going to assume you want everything that happens during the date of interest. So you want everything from and including midnight of that day and before midnight of the next day.
declare
AsOf Date = date '2015-01-01 13:14:15';
select ...
from tablename
where tabledate >= trunc( AsOf )
and tabledate < trunc( AsOf ) + 1;
If you know the date doesn't have a time portion, just can eliminate the calls to trunc. But you may want to keep them just in case.
I want to say the follow but substitute the date with SYSDATE but the time between is what I want to adjust. What would the syntax be?
where mydatefield between SYSDATE+'0001' and SYSDATE+'2359'
...
WHERE TO_CHAR( MOPACTIVITY.MOPNOTIFICATIONSENDAT , 'yyyy-mm-dd hh24:mi' )
BETWEEN '2013-07-26 00:00:01' AND '2013-07-26 23:59:59'
;
SYSDATE (or any other date column) in Oracle has the time component. So you need to strip that off and then add the hours/minutes/time condition.
Eg. to say current day 10:00 AM to 3:00 PM, you can say
date_column between (trunc(sysdate) + 10/24) and (trunc(sysdate) + 15/24)
Oracle date arithmetic works on the day level. so, +1 will give you the next day, 1/24 will give you an hour and 10/24 will give you 10:00 AM in the current day.
SQL> alter session set nls_date_format = 'DD-Mon-YYYY HH:MI:SS AM';
Session altered.
1 select sysdate,
2 trunc(sysdate),
3 trunc(sysdate) + 10/24,
4 trunc(sysdate) + 15/24
5* from dual
SQL> /
SYSDATE 26-Jul-2013 06:26:07 PM
TRUNC(SYSDATE) 26-Jul-2013 12:00:00 AM
TRUNC(SYSDATE)+10/24 26-Jul-2013 10:00:00 AM
TRUNC(SYSDATE)+15/24 26-Jul-2013 03:00:00 PM
For your question, you seem to be interested between current day and next day, so you can try adding + 1 to the date directly, once you strip the time component.
date_column >= trunc(sysdate) and
date_column < trunc(sysdate)+1
The best way to do this is to leave your MOPACTIVITY.MOPNOTIFICATIONSENDAT as a DATE type. That allows Oracle to optimize the query if there happens to be an index on the column. I'd recommend something like this:
WHERE MOPACTIVITY.MOPNOTIFICATIONSENDAT >= TRUNC(SYSDATE)
AND MOPACTIVITY.MOPNOTIFICATIONSENDAT < TRUNC(SYSDATE) + 1
That boils down to "greater than or equal to today at midnight" and "less than tomorrow at midnight".
We can also trunc both the dates and then compare the result
where TRUNC(MOPACTIVITY.MOPNOTIFICATIONSENDAT) = TRUNC(SYSDATE)
TRUNC Removes the timestamp from the dates