Oracle - to_date() parse of 00:00:00 - sql

I am trying to format into Date from Date string by following the way,
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
The output should be,
11/19/2019 12:00:00 AM
But, I am getting the output as,
11/19/2019
When I execute following,
select to_date('11/19/2019 00:00:01', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
The correct output I am getting,
11/19/2019 12:00:01 AM
My nls_date_format is DD-MON-RR.
My nls_time_format is HH.MI.SSXFF AM.
I want output in the mentioned format. For all other values except than 00:00:00 is working fine.
Why 00:00:00 is not converting into the required format? Is there any way to achieve this?

You are getting the correct output. Only it is displayed in the default date format of your session (or database), which seems to be mm/dd/yyyy.
You control the default date format of your session with paramter nls_date_format:
alter session set nls_date_format = 'MM/DD/YYYY HH24:MI:SS';
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
Alternatively, you can use to_char() to format your date to a specific format:
select to_char(
to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS'
) as currentdate from dual;

If you want the AM format you will need this:
select to_char(to_date('19/11/2019 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM'),'dd/mm/yyyy hh:mi:ss AM')
from dual;
If you run it like this:
select to_char(to_date('19/11/2019 00:00:00','dd/mm/yyyy hh:mi:ss AM'),'dd/mm/yyyy hh:mi:ss AM')
from dual;
You will get an error.
When your date format is not 24 hours(hh24) then your value for hour needs to be between 1 and 12.
Here is the DEMO
After some research I have realised what OP wants(from his comments and after he entered some more details).
Solution for OP is:
Step 1:
ALTER session SET NLS_DATE_FORMAT = 'DD-MON-RR HH.MI.SS AM';
Step 2 now this will work:
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;

Related

Cannot convert to date, getting "ORA-01855: AM/A.M. or PM/P.M. required" error message

the following code gives me this error message - "ORA-01855: AM/A.M. or PM/P.M. required"
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
If I try to convert to char instead, I get "ORA-01722: invalid number".
SELECT TO_CHAR('2/2/2021 8:08:58 PM', 'MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
I don't have much experience with oracle database and have been struggling with it for a while. Not sure what I'm doing wrong. Thanks for any help!
It looks like you probably have NLS_DATE_LANGUAGE set to something other than English; but not NLS_LANGUAGE as that would affect the error message too:
alter session set nls_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
ORA-01855: požaduje se AM/A.M. nebo PM/P.M.
With just the date language changed:
alter session set nls_language = 'ENGLISH';
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
ORA-01855: AM/A.M. or PM/P.M. required
You can either change the session setting to English, or change the string to have the language-appropriate AM/PM indicator value:
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 ODPOLEDNE','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
NOW
---------
02-ÚNO-21
Or probably most practically and simply, override the session setting in the query:
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM','NLS_DATE_LANGUAGE=ENGLISH') "NOW"
FROM DUAL;
NOW
---------
02-ÚNO-21
db<>fiddle
You should use TO_CHAR to convert from date to char, not from char:
SELECT TO_CHAR(TO_DATE('2/2/2021 8:08:58 PM', 'MM/DD/YYYY HH:MI:SS AM'),
'MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;

conversion from string to particular date format in sql

trunc(to_date('datefield1','mm/dd/yyyy hh24:mi:ss'))
between
trunc(to_date(to_char('05/18/2016 08:57','mm/dd/yyyy hh24:mi:ss'),'mm/dd/yyyy,hh24:mi:ss'))
and
trunc(to_date(to_char('05/20/2016 08:57','mm/dd/yyyy hh24:mi:ss'),'mm/dd/yyyy,hh24:mi:ss'));
I need to convert the string '05/18/2016 08:57' into date
Why are you converting a character string to a character string? You can do:
trunc(to_date(datefield1, 'mm/dd/yyyy hh24:mi:ss'))
between trunc(to_date('05/18/2016 08:57', 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy,hh24:mi:ss') and
trunc(to_date('05/20/2016 08:57', 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss');
It is very strange to have hh24:mi:ss when the string itself has no seconds. But Oracle allows it.
That said, I would write the logic as:
datefield1 >= date '2016-05-18' and
datefield1 < date '2016-05-21'

Oracle : Date time subtraction

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';

date format conversion am to date

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?

How to convert time stored in varchar2 to 24-hour format in Oracle?

I have time as HH:MI:SS AM/PM stored in a varchar2 column in a table. How can I convert this to 24-hour format?
To convert to a DATE:
to_date(<text field>, 'DD/MM/YYYY HH:MI:SS AM')
To convert to another string:
to_char(to_date(<date field>, 'DD/MM/YYYY HH:MI:SS AM'), 'DD/MM/YYYY HH24:MI:SS')
e.g. (with NLS_DATE_FORMAT set to YYYY-MM-DD HH24:MI:SS):
select to_date('09/08/2013 5:13:07 PM', 'DD/MM/YYYY HH:MI:SS AM'),
to_char(to_date('09/08/2013 5:13:07 PM', 'DD/MM/YYYY HH:MI:SS AM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
TO_DATE('09/08/2013 TO_CHAR(TO_DATE('09
------------------- -------------------
2013-08-09 17:13:07 09/08/2013 17:13:07
If you only have the time portion:
select to_date('5:13:07 PM', 'HH:MI:SS AM'),
to_char(to_date('5:13:07 PM', 'HH:MI:SS AM'), 'HH24:MI:SS')
from dual;
TO_DATE('5:13:07PM' TO_CHAR(
------------------- --------
2013-08-01 17:13:07 17:13:07
Notice that if you don't provide the date part of the value it defaults to the first day of the current month (mentioned in the documentation for datetime literals; but if you only have the time you probably want to keep it as a string anyway.
Use this: 2013-08-01 17:13:07 17:13:07
Notice that if you don't provide the date part of the value it defaults to the first day of the current month (mentioned in the documentation for datetime literals), but if you only have the time you probably want to keep it as a string anyway.