Oracle Time Zones Conversion (using from_tz) - oracle

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;

Related

VARCHAR2 to TIMESTAMP conversion in ORACLE 12c

I have a column in table A as
select create_time from table_a;
The value is
08-MAR-19 08.23.47.897000000 PM GMT.
This column has been marked as VARCHAR2 for some business purpose. Now I am trying to get this column and convert the value to TIMESTAMP for some purpose, like this as below:
SELECT TO_TIMESTAMP(create_time, 'DD-MON-YYYY HH.MI.SS.FF AM') from table_a;
But I am getting error:
ORA-01830: date format picture ends before converting entire input string
Can someone help me to convert this varchar data to timestamp. The reason I am trying to do is, I need to convert this time from one timezone to another :
eg:
SELECT FROM_TZ(TO_TIMESTAMP(create_time, 'DD-MON-YYYY HH.MI.SS.FF AM'), 'UTC') AT TIME ZONE 'CET' from table_a;
SQL> select replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','') AS RESULT from dual
;
RESULT
--------------------------------
08-MAR-19 08.23.47.897000000 PM
SQL> select to_timestamp(replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','')) as RESULT from dual ;
RESULT
---------------------------------------------------------------------------
08-MAR-19 08.23.47.897000000 PM
SQL> select from_tz(to_timestamp(replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','')),'UTC') AT TIME ZONE 'CET' AS RESULT from dual ;
RESULT
---------------------------------------------------------------------------
08-MAR-19 09.23.47.897000000 PM CET
SQL>
The four characters GMT at the end are not accounted for in your format string
SELECT TO_TIMESTAMP('08-MAR-19 08.23.47.897000000 PM GMT', 'DD-MON-YYYY HH.MI.SS.FF AM') from dual;
The following takes care of that:
SELECT TO_TIMESTAMP(substr(create_time, 1, LENGTH(create_time) -4), 'DD-MON-YYYY HH.MI.SS.FF AM') t from dual;
Try this.
select TO_TIMESTAMP( REPLACE(ts, 'GMT', '')) from test_timestamp;
Remove GMT using REPLACE
WITH A AS (SELECT REPLACE('08-MAR-19 08.23.47.897000000 PM GMT','GMT','') AS D FROM DUAL)
SELECT TO_TIMESTAMP (D, 'DD-MON-YYYY HH.MI.SS.FF AM')
FROM A
Doing something like:
to_timestamp(replace(create_time ,'GMT', null))
relies on your NLS settings, both for the timestamp format - particularly that it has an RR year mask - and the language for the month abbreviation. It would be safer to do:
to_timestamp(replace(create_time, ' GMT', null),
'DD-MON-RR HH.MI.SS.FF AM', 'NLS_DATE_LANGUAGE=ENGLISH')
If the time zone isn't always GMT, but is always a valid and recognised region (not BST, for instance) then you might want to preserve the full date/time including that zone:
to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH')
If you want that as a plain timestamp you can cast it, possibly changing to a specific zone first:
cast(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH') as timestamp)
or
cast(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH') at time zone 'Asia/Tokyo' as timestamp)
or normalised to UTC (which won't affect GMT values, of course, as they're essentially the same):
sys_extract_utc(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH'))
db<>fiddle

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 format doesn't show time

I have simple calculation, I subtract interval from date with time:
select TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') from dual;
It works fine, the result: 2016-12-05 22:59:59
but it doesn't work correctly with timezones, so the next approach solves the problem with timezone. I just wrap expression with to_date() one more time
select TO_DATE(
TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00')) from dual;
but now it turns time to zeros. Result should be: 2016-12-05 22:59:59 but actual: 2016-12-05 00:00:00
If I add format to the outer to_date as this:
select to_date( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS') from dual;
The result become very strange: 0005-12-16 00:00:00
What I'm doing wrong?
DATE data type does not support any time zone functions, you must use TIMESTAMP WITH TIME ZONE for that.
Your query
SELECT TO_DATE( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS')
FROM dual;
does following:
Create a DATE '2016-12-05 23:04:59'
Subtract interval '00 0:05:00'
Cast to a VARCHAR2 (using NLS_DATE_FORMAT format)
Cast to a DATE using YYYY-MM-DD HH24:MI:SS format
In case your NLS_DATE_FORMAT would be equal to YYYY-MM-DD HH24:MI:SS this query returns correct output.
Use this one:
SELECT TO_TIMESTAMP('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE(... works as well. If you need time zone support you must do:
SELECT TO_TIMESTAMP_TZ('2016-12-05 23:04:59 Europe/Berlin', 'YYYY-MM-DD HH24:MI:SS TZR') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE( char, fmt, nls ) takes VARCHAR2 arguments.
Performing TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') returns a DATE datatype which when you pass it to TO_DATE() oracle will cast it to a VARCHAR2 datatype so it matches the expected datatype of the argument (implicitly calling TO_CHAR( value, NLS_DATE_FORMAT ) to perform this cast) and then convert this back to a DATE datatype.
You just need to do:
SELECT TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS')
- to_dsinterval('00 0:05:00')
FROM DUAL;
If you want to handle time zones then use a TIMESTAMP AT TIME ZONE and just convert it to whatever timezone you want to store the date at:
SELECT TIMESTAMP '2016-12-05 23:04:59 Europe/Paris' AT TIME ZONE 'UTC'
FROM DUAL;
(Will create your timestamp in Paris' time zone and convert it to the correct time in the UTC time zone).

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

How to strip Timestamp up to just seconds?

Given a timestamp value, i.e. "04-SEP-14 03.09.09.272949000 PM", how do I get the date time up to just the seconds part and zeroing out the micro-seconds part?
given: 04-SEP-14 03.09.09.272949000 PM
wish: 04-SEP-14 03.09.09.00 PM
thanks in advance.
You can use the cast function to cast it to a zero-millisecond TIMESTAMP with the construct:
cast(<your column>, timestamp(0))
Using a CTE to pass in your value:
with t as (
select to_timestamp('04-SEP-14 03.09.09.272949000 PM',
'DD-MON-RR HH:MI:SS.FF AM') as tstamp
from dual
)
select to_char(cast(tstamp as timestamp(0)), 'DD-MON-RR HH:MI:SS.FF2 AM')
from t;
TO_CHAR(CAST(TSTAMPASTIMESTAMP(0)),'DD-M
----------------------------------------
04-SEP-14 03:09:09.00 PM
You could also cast it twice, once to DATE to strip the milliseconds, and then back to TIMESTAMP again:
with t as (
select to_timestamp('04-SEP-14 03.09.09.272949000 PM',
'DD-MON-RR HH:MI:SS.FF AM') as tstamp
from dual
)
select to_char(cast(cast(tstamp as date) as timestamp),
'DD-MON-RR HH:MI:SS.FF2 AM')
from t;
TO_CHAR(CAST(CAST(TSTAMPASDATE)ASTIMESTA
----------------------------------------
04-SEP-14 03:09:09.00 PM
... which is doing more work as well as being more typing.
You can't use trunc(date) because that implicitly converts the TIMESTAMP to a DATE, and since a DATE is already at that precision you can't pass SS as the fmt anyway.

Resources