I'm running two queries
select TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS PM') from dual;
It displays date with exact time.
select TO_DATE(SYSDATE, 'DD-MON-YYYY HH:MI:SS PM') from dual;
It displays date with default time 12:00:00 AM.
I do not understand TO_CHAR and TO_DATE usage. How to display date with exact time by using TO_DATE
to_char function is used to convert the given data into
character....
SQL> SELECT TO_CHAR(SYSDATE, 'dd/mm/yyyy') FROM dual;
TO_CHAR(SY
------------------
04/04/2012
to_date is used to convert the given data into date data
formate data type....
eg: to_date('070903', 'MMDDYY') would return a date value
of July 9, 2003.
Reference: Interview Question
Related
i simply wanna change a string to a date format using to_date
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MOM-YY HH24:MI:SS AM') FROM DUAL;
and also i want to change to 24 format
when i run this i get the ORA-01821: date format not recognized error .
The correct format for converting your string to a date is:
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MON-YY HH.MI.SS AM')
FROM DUAL;
If you want it as a string, then you can use TO_CHAR() after converting to a date. That said, I recommend keeping the value as a date.
The correct format is
SELECT TO_CHAR(TO_DATE('20/APR/20 09.50.06 AM' , 'DD-MON-YY HH:MI:SS AM'),'DD-MON-YY HH:MI:SS AM') FROM DUAL;
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)
I have time stamp in the CREATE_DATE column of my table in the below format.
9/8/2016 5:37:35 AM
I need to search the records between specific CREATE_DATE range. Please help me on how to do the same.
I have used the below query but getting
ORA-01843: not a valid month
select *
from gdf.msg_pyld
where dbms_lob.instr(pyld_clob,'4861615654')>=1
and CREATE_DATE BETWEEN '09/08/2016 5:59:17 AM' AND '09/08/2016 5:59:17 PM';
What you tried is a comparison of a date (a numeric type with a very special meaning) against STRINGS. This will never work.
If CREATE_DATE is indeed a DATE in Oracle, then this should work:
... AND CREATE_DATE BETWEEN to_date('09/08/2016 5:59:17 AM', 'mm/dd/yyyy hh:mi:ss AM')
AND to_date('09/08/2016 5:59:17 PM', 'mm/dd/yyyy hh:mi:ss PM')
assuming 09/08 means September 8; if instead it is supposed to mean 9 August, then change mm/dd to dd/mm in the format models. Good luck!
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';
I need a help on how to convert '09-07-15 07:41:01AM' to '2015-07-09 07:41:01'.
I have tried below query but it is not giving accurate result.
select to_char(to_date('09-07-15 07:41:01AM'
,'DD-MM-YYYY HH:MI:SS AM')
,'YYYY-MM-DD HH24:MI:SS') from dual
Have you tried
select to_char(add_months(to_date('09-07-15 07:41:01AM','DD-MM-YY HH:MI:SS AM'),24000),'YYYY-MM-DD HH24:MI:SS') from dual
It looks like you have to many Y's, and your M's ans D's are inconsistent!
I'm not sure if it should be YY-MM-DD or YY-mm-dd?