How to convert a string to datetime - oracle

I have a string like 'Jul 18, 2013 11:06:23 AM'
I want to display it like '07/18/2013 11:06:23 AM'
I try to to use To_date function (oracle function) to convert it, however I got error.
Please help me!

Try this, it works very well:
select to_char(to_date('Jul 18, 2013 11:06:23','MON DD, YYYY HH:MI:SS',
'NLS_DATE_LANGUAGE = American'),'MM/DD/YYYY HH:MI:SS AM') from dual
You can adapt to your problem.

You want to use the following conversion function
to_date(mystring, 'Mon DD, YYYY HH:MI:SS AM')
where your string is like in the above format, you have to use the format string based on your input string.
If you want to display the date in above format you have to use TO_CHAR function like below
select to_char(mydate, 'DD/MM/YYYY HH:MI:SS AM') from yourtable
check here for more information http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions180.htm

Related

Trying to covert timestamp to date causes error

select to_date('25-MAY-20 12.10.12.320000 PM', 'dd-mon-yy hh:mi:ss pm') from dual;
getting the error
"am or pm required"
Oracle only stores precision up to seconds in a date type. If you want fractional seconds (e.g. milliseconds) you will need to use a timestamp column. But in any case, your current format mask is wrong. Try this instead:
SELECT TO_TIMESTAMP('25-Feb-20 12.10.12.320000 PM', 'dd-mon-yy hh.mi.ss.ff6 pm')
FROM dual
Demo
You can convert your timestamp into char and then convert to date.
Example with systimestamp:
select to_date(
to_char(systimestamp,'dd-mon-yy hh:mi:ss PM')
, 'dd-mon-yy hh:mi:ss PM')
from dual;

Oracle to_date with p.m./a.m

I need to convert a string into a Date in oracle.
The format of the string is like this:
'08/11/1999 05:45:00 p.m.'
But the last position can change p.m or a.m.
I tried to do some like:
to_date('08/11/1999 05:45:00 p.m.', 'dd/mm/yyyy hh:mi:ss a.m./p.m.')
to_date('08/11/1999 05:45:00 p.m.', 'dd/mm/yyyy hh:mi:ss am/pm')
But return me an error ORA-01855 : AM/A.M. or PM/P.M. required... any idea ?
Try this:
to_date
( '08/11/1999 05:45:00 p.m.'
, 'dd/mm/yyyy hh:mi:ss a.m.'
, 'nls_date_language=american'
)
It seems that "a.m." and "p.m." rather than "am" and "pm" require nls_date_language to be set to "american".
to convert time for am and pm, just give a.m. like below
to_date(UPPER('08/11/1999 05:45:00 p.m.'),'dd/mm/yyyy hh:mi:ss a.m.')
hope this might help. please refer https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm
I tried this case. Here is the right answer:
TO_DATE('3/9/2022 9:35:00 PM', 'MM/DD/YYYY hh:mi:ss am', 'nls_date_language=american')

Oracle Time Zones Conversion (using from_tz)

I'm trying to convert a time (date + time) from one time zone to another. In the query below, I'm trying to convert a time from EST ("America/New_York") to PST ("America/Los_Angeles"). The query is partially working; the results:
DATABASE_DATE = 2012-02-13 1:00:00 PM
LOCALTIME (what I get): 2012-02-12 10:00:00 AM.
So the time is good but the date is wrong. It should be 2012-02-13 instead of 2012-02-12.
Am I doing something wrong? Here's my query:
select to_date( to_char( ( from_tz( to_timestamp( DATABASE_DATE
, 'YYYY-MM-DD HH:MI:SS')
,'America/New_York')
at time zone 'America/Los_Angeles')
,'YYYY-MM-DD HH:MI:SS')
,'YYYY-MM-DD HH:MI:SS') as localtime
from table
Thanks
to_timestamp() gets a string (VARCHAR2, CHAR ...) if you try to give it a date, then oracle will convert it to a string according to NLS_DATE_FORMAT which might vary in different environments and return unexpected results (as in this case).
What you should do is use to_char first, so your query can look like this:
select to_date(to_char((from_tz(to_timestamp(to_char(DATABASE_DATE, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/New_York')
at time zone 'America/Los_Angeles'),'YYYY-MM-DD HH:MI:SS PM'),'YYYY-MM-DD HH:MI:SS PM') as localtime
from table
UPDATE: if I understand you right then you want something like this:
select to_char((from_tz(to_timestamp(to_char(DATABASE_DATE, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/New_York')
at time zone 'America/Los_Angeles'),'YYYY-MM-DD HH:MI:SS PM TZD') as localtime
from table
SELECT TO_CHAR(NEW_TIME(systimestamp,'EST','PST'), 'DD-MON-YY HH24:MI:SS') AS converted_timestamp_column FROM DUAL;
Please try this as well .There is a function for this purpose in oracle
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2036.htm
select NEW_TIME (TO_DATE ('2011/11/11 01:45', 'yyyy/mm/dd HH24:MI'), 'AST', 'MST') from dual;

how to convert a string date to date format in oracle10g

My date value is stored as varchar2 and the value is 15/August/2009,4:30 PM, how to convert this to a proper date format like DD-MM-YYYY.
You can convert a string to a DATE using the TO_DATE function, then reformat the date as another string using TO_CHAR, i.e.:
SELECT TO_CHAR(
TO_DATE('15/August/2009,4:30 PM'
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM DUAL;
15-08-2009
For example, if your table name is MYTABLE and the varchar2 column is MYDATESTRING:
SELECT TO_CHAR(
TO_DATE(MYDATESTRING
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM MYTABLE;
You need to use the TO_DATE function.
SELECT TO_DATE('01/01/2004', 'MM/DD/YYYY') FROM DUAL;

Convert VARCHAR2 to TIMESTAMP in Oracle

I have a VARCHAR2 value in the format '14-SEP-11 12.33.48.537150 AM' and I need to convert this to a TIMESTAMP as is. That is like,
SELECT TO_DATE('14-SEP-11 12.33.48.537150 AM', '<format_string>') FROM DUAL;
I want the return from the above query to be '14-SEP-11 12.33.48.537150 AM' in TIMESTAMP data format.
What should be the 'format_string' for this.
Please help since I tried many things but none works.. :(
I am using Oracle 11gR2.
Thanks.
DATE and TIMESTAMP are two different datatypes. So you need to use the correct conversion function for each, which in your case would be TO_TIMESTAMP().
SELECT TO_TIMESTAMP('14-SEP-11 12.33.48.537150 AM', 'DD-MON-RR HH:MI:SS.FF AM')
FROM DUAL;
select from_tz(to_timestamp('14-SEP-11 12.33.48.537150 PM', 'DD-Mon-RR HH.MI.SS.FF AM'), 'EUROPE/LONDON') from dual

Resources