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')
Related
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;
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
I have a varchar column and I want to convert yyyy-mm-dd hh:mi:ss into mm-dd-yyyy hh:mi:ss and cast it into a date. How can it be done?
To convert the string into a date:
to_date (the_string, 'yyyy-mm-dd hh:mi:ss')
If you then want that to be formatted as a string in format mm-dd-yyyy hh:mi:ss:
to_char (to_date (the_string, 'yyyy-mm-dd hh:mi:ss'), 'mm-dd-yyyy hh:mi:ss')
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;
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