problem with using to_date in oracle query - sql

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;

Related

SQL Date conversion getting "Invalid Number"

Hello I have the following date in SQL ORCLE
2020-02-07 13:21:56.478000
And I want it in the following format:
27-JAN-20 03.00.00.000000000 AM
I googled around and found the to_char and to_date and came up with this
(TO_TIMESTAMP(TO_CHAR('2020-02-07 13:21:56.478000','DD-MON-YY HH:MI:SS.FF'))
But I keep getting "invalid number" and oracle is not telling me where it is. Any ideas?
Use the following method
to_char(
timestamp '2020-02-07 13:21:56.478000',
'DD-MON-YY HH:MI:SS.FF'
)
The problem is that to_char() expects a date-like datatype as argument, while your are giving it a string.
If you are dealing with a literal string, then you use the date literal syntax:
to_char(
timestamp '2020-02-07 13:21:56.478000',
'DD-MON-YY HH:MI:SS.FF AM'
)
If you are dealing with a string column, then you can turn it to a timestamp first with to_timestamp():
to_char(
to_timestamp(my_string_col, 'YYYY-MM-DD HH24:MI:SS.FF'),
'DD-MON-YY HH:MI:SS.FF AM'
)
Finally, if you are dealing with a timestamp column, then you can use to_char() directly:
to_char(my_date_col, 'DD-MON-YY HH:MI:SS.FF AM')
Note: your original format modifier was missing the AM/PM part, I added it.

SQL Query Error: date format picture ends before converting entire input into string

I'm getting an Error when I run the query below:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-Mon-yy hh24.mi.ss')
Date format picture ends before converting entire input into string
Can I get an assistance please
The major problem you've got is that your date-and-time string can't be parsed using TO_DATE - you'll need to use TO_TIMESTAMP. The issue is that TO_DATE doesn't recognize the FFn format specifier, which is used to process fractional seconds. This makes sense because DATE values are only accurate to the second. So you'll need to use
TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM')
Which will return a TIMESTAMP value. If you really need this to be a DATE rather than a TIMESTAMP you can cast the value to DATE by using
CAST(TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM') AS DATE)
dbfiddle here
You can directly use to_date function and miliseconds can be ignored using # as following:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.###### AM')
Number of # is equal to number of 0s after dot(.)
db<>fiddle demo
Cheers!!

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

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?

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.