Oracle equivalent of teradata timezones - oracle

I am trying to convert the following Teradata SQL to Oracle. and the problem is that I am not able to find the equivalent time zone value in Oracle. In the below example, 'Europe Central' is not recognized in Oracle.
select hi.create_date
, to_char( hi.create_date,'YYYY-MM-DD HH24:MI:SS')
, ((CAST(to_char( hi.create_date,'YYYY-MM-DD HH24:MI:SS')||'+00:00' AS TIMESTAMP(0))) at time zone 'Europe Central')
from historical_info hi
This below code in Oracle throws an error:
SELECT create_date,
CAST( create_date
AS TIMESTAMP WITH TIME ZONE
) AT TIME ZONE 'Europe Central'
TZ_LOSANG
FROM historical_info
ORA-01878: specified field not found in datetime or interval.
01878. 00000 - "specified field not found in datetime or interval"
*Cause: The specified field was not found in the datetime or interval.
*Action: Make sure that the specified field is in the datetime or interval.
Can you please help me convert the Teradata time zones to Oracle recognized time zones.

You can get a list of supported time zones, as shown in the documentaion:
You can obtain a list of time zone names and time zone abbreviations from the time zone file that is installed with your database by entering the following statement:
SELECT TZNAME, TZABBREV
FROM V$TIMEZONE_NAMES
ORDER BY TZNAME, TZABBREV;
There isn't a simple built-in way to convert the Teradata name to an Oracle name, so you'll need to pick a suitable equivalent for each zone/region you have to deal with.
Oracle doesn't have a 'Europe/Central' time zone name, but does recognise the CET abbreviation, or any of the names which map to that abbreviation:
alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS TZR';
with historical_info(create_date) as (select sysdate from dual)
SELECT create_date,
CAST(create_date AS TIMESTAMP) AT TIME ZONE 'CET' TZ_AT_CET
FROM historical_info;
CREATE_DATE TZ_AT_CET
------------------- -----------------------
2015-08-17 11:59:29 2015-08-17 12:59:29 CET
But that is adjusting the time from my session time zone to CET, which may not be what you want. If you are saying that the stored time represents CET then you want the from_tz function:
SELECT create_date,
CAST(create_date AS TIMESTAMP) AT TIME ZONE 'CET' TZ_AT_CET,
FROM_TZ(CAST(create_date AS TIMESTAMP), 'CET') TZ_FROM_CET
FROM historical_info;
CREATE_DATE TZ_AT_CET TZ_FROM_CET
------------------- ----------------------- -----------------------
2015-08-17 12:01:40 2015-08-17 13:01:40 CET 2015-08-17 12:01:40 CET
From your column alias you may be trying to show that CET time in a different zone, which needs both steps:
SELECT create_date,
FROM_TZ(CAST(create_date AS TIMESTAMP), 'CET') TZ_FROM_CET,
FROM_TZ(CAST(create_date AS TIMESTAMP), 'CET')
AT TIME ZONE 'America/Los_Angeles' TZ_LOSANG
FROM historical_info
CREATE_DATE TZ_FROM_CET TZ_LOSANG
------------------- ----------------------- ---------------------------------------
2015-08-17 12:02:55 2015-08-17 12:02:55 CET 2015-08-17 03:02:55 AMERICA/LOS_ANGELES
If you have rows with different zones and those are stored as a separate column at the moment, you can use a case expression (or decode) to specify the Oracle equivalent for each one; but you'll still have to do this mapping yourself. You could put the translation into a look-up table if this isn't a one-off task.
with historical_info(create_date, orig_zone) as (
select sysdate, 'Europe Central' from dual
union all select sysdate, 'Europe Western' from dual
union all select sysdate, 'America Central' from dual
)
SELECT create_date,
FROM_TZ(CAST(create_date AS TIMESTAMP),
case orig_zone
when 'Europe Central' then 'CET'
when 'Europe Western' then 'WET'
when 'America Central' then 'US/Central'
-- when x then y for all other values you need
end) TZ_ADJUSTED
FROM historical_info
CREATE_DATE TZ_ADJUSTED
------------------- --------------------------------------
2015-08-17 12:16:13 2015-08-17 12:16:13 CET
2015-08-17 12:16:13 2015-08-17 12:16:13 WET
2015-08-17 12:16:13 2015-08-17 12:16:13 US/CENTRAL
You need to be careful to use time zones (or abbreviations) that adjust for daylight savings appropriately.

Related

How to convert Time string in UTC to CST in Oracle

I need to convert timestamp String in UTC TZ format to CST TZ format as shown in here "2019-01-02T11:53:59.269-05:00"
So basically i need the output of this query with SYSTIMESTAMP replaced with time String in UTC TZ Format.
select TO_CHAR(SYSTIMESTAMP ,'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') CREATEDTIME from dual;
I tried lot of stuff but getting errors
select TO_CHAR( to_timestamp('2019-01-02 11:53:59.759', 'YYYY-MM-DD HH24:MI:SS.FF3TZH') ,'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') CREATEDTIME from dual
Error
ORA-01821: date format not recognized
Could you please help me write the correct SQL query.
With a TSTZ, you can select at a different time-zone.
Below are some examples for daylight- and standard-time.
Standand-Time:
SELECT TO_CHAR(TO_TIMESTAMP_TZ('2019-01-02T16:53:59.269 UTC',
'YYYY-MM-DD"T"HH24:MI:SS.FF3 TZR') AT TIME ZONE 'AMERICA/CHICAGO',
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM')
AS CENTRAL_TIME
FROM DUAL;
Result:
CENTRAL_TIME
2019-01-02T10:53:59.269-06:00
1 row selected.
And a daylight-savings example:
SELECT TO_CHAR(TO_TIMESTAMP_TZ('2019-04-02T17:35:52.136 UTC',
'YYYY-MM-DD"T"HH24:MI:SS.FF3 TZR') AT TIME ZONE 'AMERICA/CHICAGO',
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM')
AS CENTRAL_TIME
FROM DUAL;
Result:
CENTRAL_TIME
2019-04-02T12:35:52.136-05:00
1 row selected.

Time difference with daylight savings oracle

We are trying to perform arithmetic on a date. We are trying to add 1 day (24 hours) Here's a basic example...
select to_date('2018-10-06 22:00:00') + 1 from dual;
The result we get back from this is as follows...
2018-10-07 22:00:00
That seems good on paper, however we are in the Sydney timezone and during that interim period daylight savings occurs so the time should like something more like this...
2018-10-07 23:00:00
So I suppose my question is, when doing arithmetic in oracle is it possible to take into consideration daylight savings time. One idea was to convert the initial date to UTC and convert back to SYDNEY time, but it seems like a complicated solution that might already be handled by oracle functionality somewhere.
Cheers
You can't really do it directly with a date as those are not timezone aware.
You can start with a timestamp with time zone, e.g. as a timestamp literal:
select timestamp '2018-10-06 22:00:00 Australia/Sydney' + interval '1' day
from dual;
TIMESTAMP'2018-10-0622:00:00AUSTRALIA/SYDNEY'+
----------------------------------------------
2018-10-07 23:00:00.000000000 AUSTRALIA/SYDNEY
which you can cast back to a date if you want to discard that information:
select cast(timestamp '2018-10-06 22:00:00 Australia/Sydney' + interval '1' day as date)
from dual;
CAST(TIMESTAMP'2018
-------------------
2018-10-07 23:00:00
If you're starting from a date, e.g. in a table column, you can cast that to a timestamp and specify the time zone:
with your_table (your_date) as (
select to_date('2018-10-06 22:00:00') from dual
)
select from_tz(cast(your_date as timestamp), 'Australia/Sydney') + interval '1' day
from your_table;
FROM_TZ(CAST(YOUR_DATEASTIMESTAMP),'AUSTRALIA/
----------------------------------------------
2018-10-07 23:00:00.000000000 AUSTRALIA/SYDNEY
and optionally cast back to date again to discard the fractional seconds and time zone information.
If your session time zone is Sydney then you could let that be set implicitly by casting to timestamp with time zone instead:
with your_table (your_date) as (
select to_date('2018-10-06 22:00:00') from dual
)
select cast(your_date as timestamp with time zone) + interval '1' day
from your_table;
but it's generally better not to assume anything about the session environment that will run your code.
When the clocks go back at the end of DST you can specify which of the two possible values of times between 02:00 and 03:00 you mean by including a DST indicator:
alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS TZH:TZM';
select timestamp '2018-04-01 01:00:00 Australia/Sydney AEDT' from dual;
TIMESTAMP'2018-04-0101:00:
--------------------------
2018-04-01 01:00:00 +11:00
select timestamp '2018-04-01 02:00:00 Australia/Sydney AEDT' from dual;
TIMESTAMP'2018-04-0102:00:
--------------------------
2018-04-01 02:00:00 +11:00
select timestamp '2018-04-02 02:00:00 Australia/Sydney AEST' from dual;
TIMESTAMP'2018-04-0202:00:
--------------------------
2018-04-02 02:00:00 +10:00
select timestamp '2018-04-01 03:00:00 Australia/Sydney AEST' from dual;
TIMESTAMP'2018-04-0103:00:
--------------------------
2018-04-01 03:00:00 +10:00
Although for me this only seems to work in 12.2.0.1; in earlier versions AEST/AEDT aren't recognised (though EST is). I'm not sure if that's just down the time zone file version - my 12.2 instance have version 26.
Going the other way if you have times that appear to be in the hour that doesn't exist when the clocks go forward at the start of DST, there isn't much you can do about it as they are invalid. If you populate fields using sysdate and the server is set to Sydney, or current_date and the session is, then that shouldn't happen. Dates created with to_date() could be in that window, but then you probably need to figure out why and stop that happening.

Oracle - Converting a UTC timestamp to a localtime (GMT/BST) timestamp

I have to create convert a datetime as UTC into localtime (GMT/BST)
The dates in the database are UTC AND the database is set to UTC.
I believe I can get the offset between UTC and (say) BST using TZ_OFFSET, but how can I then use that to convert the UTC datetime into a BST datetime?
So, for example, if the database (UTC) datetime is
'2018-04-03 14:30:00'
And the offset is '+01:00'
I would expect the result to be
'2018-04-03 15:30:00'
If there an elegant way of doing this? Rather than using grungy arithmetic (which then has to take into account midnight, end of month, end of year etc.)
Thanks
Use FROM_TZ to create a TIMSTAMP WITH TIME ZONE, then the conversion is very simple, for example:
FROM_TZ({your column}, 'UTC') AT TIME ZONE 'Europe/London'
As you can see in the documentation:
Example of Converting Time Zones With the AT TIME ZONE Clause:
SELECT FROM_TZ(CAST(TO_DATE('1999-12-01 11:00:00',
'YYYY-MM-DD HH:MI:SS') AS TIMESTAMP), 'America/New_York')
AT TIME ZONE 'America/Los_Angeles' "West Coast Time"
FROM DUAL;
West Coast Time
----------------------------------------------------------
01-DEC-99 08.00.00.000000 AM AMERICA/LOS_ANGELES
So, applied to your case scenario:
SELECT FROM_TZ(CAST(TO_DATE('2018-04-03 14:30:00',
'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP), 'UTC')
AT TIME ZONE 'GMT' "Greenwich Mean Time"
FROM DUAL;
Greenwich Mean Time
----------------------------------------------------------
03-APR-18 02.30.00.000000 PM GMT
You can get the list of available timezones with:
SELECT tzname, tzabbrev FROM V$TIMEZONE_NAMES;
If you have a table YOUR_TABLE with a column some_timestamp:
create table YOUR_TABLE (
some_timestamp timestamp
);
/
insert into YOUR_TABLE (
SOME_TIMESTAMP
) values (
CAST(TO_DATE('2018-04-03 14:30:00', 'YYYY-MM-DD HH24:MI:SS') AS TIMESTAMP)
);
Then you can run:
select
SOME_TIMESTAMP,
FROM_TZ(SOME_TIMESTAMP, 'UTC') AT TIME ZONE 'GMT' "Greenwich Mean Time",
FROM_TZ(SOME_TIMESTAMP, 'UTC') AT TIME ZONE 'Europe/London' "London Time"
from YOUR_TABLE;
SOME_TIMESTAMP Greenwich Mean Time London Time
----------------------------------------------------------
03-APR-18 02.30.00.000000 PM 03-APR-18 02.30.00.000000 PM GMT 03-APR-18 03.30.00.000000 PM EUROPE/LONDON
And if you are absolutely sure that your server is in UTC, as well as the timestamps introduced, then you can skip that UTC conversion part:
select
SOME_TIMESTAMP,
SOME_TIMESTAMP AT TIME ZONE 'GMT' "Greenwich Mean Time",
SOME_TIMESTAMP AT TIME ZONE 'Europe/London' "London Time"
from YOUR_TABLE;
it should be pretty simple::
select datetimecolumn + INTERVAL '1' HOUR from mytable
hope i understand your requirement correctly.

Convert UTC time to specific time zone in oracle

I have to convert UTC time to some specific time zone, I have converted the sysdate to UTC,now I want to convert it to time zone specific, This is how I am converting sysdate to UTC
select cast(sys_extract_utc(systimestamp) as DATE) from dual;
I tried writing +5 before from keyword,but its returning the date after 5 days.
select cast(sys_extract_utc(systimestamp) as DATE)+5 from dual;
I have get the timezone offset with below query, but I have clue to use this in above query
SELECT TZ_OFFSET('US/Eastern') FROM DUAL;
How can I convert the UTC time to some timezone offset,something like below
select cast(sys_extract_utc(systimestamp) as DATE) from dual where TZ_OFFSET=+5;
You can get the current UTC time using:
SELECT CAST( SYSTIMESTAMP AT TIME ZONE 'UTC' AS DATE )
FROM DUAL;
You can reverse the process to change a UTC DATE to a TIMESTAMP WITH TIME ZONE at +04:00 using:
SELECT FROM_TZ( CAST( your_date AS TIMESTAMP ), 'UTC' ) AT TIME ZONE '+04:00'
FROM your_table
Trying to add on the time zone offset would give you problems with daylight savings adjustments.
You can use the at time zone syntax to specify a conversion, but you don't need to convert to UTC first. If you do you might not get what you expect, without taking an extra step:
select systimestamp as sys_ts,
sys_extract_utc(systimestamp) as utc_ts,
sys_extract_utc(systimestamp) at time zone 'US/Eastern' as edt_ts
from dual;
SYS_TS UTC_TS EDT_TS
------------------------------ ----------------------- ----------------------------------
2017-10-05 11:30:41.023 +01:00 2017-10-05 10:30:41.023 2017-10-05 05:30:41.023 US/EASTERN
That says the time in New York is 05:30, when it's actually 06:30 at time of writing.
The sys_extract_utc function gives you the UTC equivalent of your system time, but with on embedded time zone info - it's a plain timestamp, not a timestamp with time zone. So when you adjust it, it's implicitly converted to the system time zone with no adjustment, leaving you with the wrong actual time.
You can specify that the extracted value is UTC using the from_tz() function:
select systimestamp as sys_ts,
sys_extract_utc(systimestamp) as utc_ts,
from_tz(sys_extract_utc(systimestamp), 'UTC') at time zone 'US/Eastern' as edt_ts
from dual;
SYS_TS UTC_TS EDT_TS
------------------------------ ----------------------- ----------------------------------
2017-10-05 11:30:41.144 +01:00 2017-10-05 10:30:41.144 2017-10-05 06:30:41.144 US/EASTERN
But you don't need to do that much work, you can just take the original time zone-aware systimestamp value and apply at time zone directly to that:
select systimestamp as sys_ts,
systimestamp at time zone 'US/Eastern' as edt_ts
from dual;
SYS_TS EDT_TS
------------------------------ ----------------------------------
2017-10-05 11:30:41.271 +01:00 2017-10-05 06:30:41.271 US/EASTERN
Which you can then cast to a date data type if that is a requirement:
cast(from_tz(sys_extract_utc(systimestamp), 'UTC') at time zone 'US/Eastern' as date)
or more simply:
cast(systimestamp at time zone 'US/Eastern' as date)

Oracle Timestamp, Max and Minimal Values

I was searching, also in the Oracle Doc, for the following:
What is the range for Timestamp in Oracle?
I know for date it is -4712, Jan-01 to 9999 Dec-31, but what for Timestamp?
Anyone a clue or hint where I can search?
You can always just try it:
SQL> select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' ) from dual;
TO_TIMESTAMP('9999-12-3123:59:59','YYYY-MM-DDHH24:MI:SS')
---------------------------------------------------------------------------
31-DEC-99 11.59.59.000000000 PM
and:
SQL> select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )+1 from dual;
select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )+1 from dual
*
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
It would be surprising if the range for the DATE portion of a TIMESTAMP was smaller than the range for a DATE, so it should be:
-4712-01-01 00:00:00 to 9999-12-31 23:59:59.999999
That assumes no time zone; the UTC value is probably constrained to that range, but someone in an Eastern time zone might manage to see a data value on 1000-01-01 in their time zone.
It is hard to find definitive data off Oracle's site. The best I found in a casual survey was:
http://www.techonthenet.com/oracle/datatypes.php
There are probably others.
I found a quote which says:
TIMESTAMP Datatype
The TIMESTAMP datatype is an extension of the DATE datatype. It stores the year,
month, and day of the DATE datatype, plus hour, minute, and second values.

Resources