date format conversion am to date - sql

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?

Related

problem with using to_date in oracle query

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;

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

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;

What is difference between TO_CHAR and TO_DATE

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

Converting String Date to Date Time in Oracle

I have a situation where I need to convert the datetime value stored as string to Timestamp:
I am using oracle database
This actually works for me select TO_DATE('11-27-2013 21:28:41', 'MM-DD-YYYY HH24:MI:SS') from dual;
But my date value now is diffent from the above:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'MM-DD-YYYY HH24:MI:SS') from dual; - failed. I have 'Sunday' inside my date.
You have to specify a correct format mask to the TO_DATE function. See a full list of format masks along with documentation for the function here: http://www.techonthenet.com/oracle/functions/to_date.php
You can correct your problem by:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY M/D/YYYY HH:MIAM') FROM DUAL;
Try using the correct format. I think this will work:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MI AM')
Here is a SQL Fiddle.
The following seems to work fine:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MIAM') FROM DUAL
Share and enjoy.

Casting String to Date

The below one is working fine and returns date in the desired format
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY'), 'DD/MM/YYYY') as last_transaction_date from table;
After adding hh24:mm:ss and error of "format code appears twice" ORA-01810 appears
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY hh24:mm:ss'), 'DD/MM/YYYY hh24:mm:ss') as last_transaction_date from table;
I'm not able to understand the difference between both cases
MI not MM for minutes
"Some people mistakenly use the MM format code to represent minutes, thus using the MM format for both the months and the minutes."
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY HH24:MI:SS'), 'DD/MM/YYYY HH24:MI:SS') as last_transaction_date from table;