from_tz - date format issue in oracle from_tz - sql

i am getting an issue with from_tz function.
select from_tz(cast(sysdate AS TIMESTAMP), 'Etc/GMT') AT TIME ZONE 'Europe/Minsk'
from dual;
output>> 26-FEB-21 05.14.22.000000000 PM EUROPE/MINSK
but when i am converting it in my desired date format, time is getting reduced with 1 hour.
select to_char(from_tz(cast(sysdate AS TIMESTAMP), 'Etc/GMT') AT TIME ZONE 'Europe/Minsk','DD-MON-YYYY HH24:MI:SS')
from dual;
output>> 26-FEB-2021 16:14:35
Can you please tell me how to get correct time in above format?

Related

Casting Local Time to UTC Formating Incorrect

HIRE_DATE is in a 'DATE' column. The timestamp is local (Los Angeles); I would like to convert it to UTC.
I can't for the life of me fathom why the UTC output is mangled (Last 2 digits of YY is the DD; and vice-versa) -- and the time does not convert to UTC.
HIRE_DATE: 30/04/2019 12:00:00 AM
select from_tz(to_timestamp(HIRE_DATE,'DD-MM-YY HH24:MI:SS'), 'America/Los_Angeles') at time zone 'UTC' from TABLE
OUTPUT: 19/04/2030 12:00:00 AM
If HIRE_DATE is a DATE data type then you don't need TO_TIMESTAMP.
TO_TIMESTAMP is used to convert a string (i.e. VARCHAR2) into a TIMESTAMP value but you have a DATE value.
Just do
select from_tz(CAST(HIRE_DATE AS TIMESTAMP), 'America/Los_Angeles') at time zone 'UTC'
from TABLE
Actually I don't understand why FROM_TZ does not accept DATE values whereas almost any other date/timestamp related function accept either DATE or TIMESTAMP value as input.
Note, the default output display format of this query is defined by current user session NLS_TIMESTAMP_TZ_FORMAT setting. If you are not satisfied with the output format, either change NLS_TIMESTAMP_TZ_FORMAT setting by executing ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = '...' or use TO_CHAR function to set output format explicitly.
Instead of
... AT TIME ZONE 'UTC'
you can also use
SYS_EXTRACT_UTC(...)
The upper returns a TIMESTAMP WITH TIME ZONE value, the second one returns a TIMESTAMP value.
Would this do any good?
SQL> select from_tz(cast (sysdate as timestamp), 'UTC') result from dual;
RESULT
---------------------------------------------------------------------------
27.09.20 10:59:28,000000 UTC
Or, in your case
select from_tz(cast (hire_date as timestamp), 'UTC' from dual
No need to apply any format mask to hire_date as it is a DATE datatype (at least, that's what you said).
You use the word "convert" which can mean one of two things:
change the data type, which is what FROM_TZ does
change the value from one time zone to another, which FROM_TZ does not do.
You didn't give your expected output, so we may misunderstand.
To change the data type:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select from_tz(cast(dte as timestamp), 'America/Los_Angeles') from data
FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES')
30-APR-19 12.00.00.000000 PM AMERICA/LOS_ANGELES
To get the simultaneous datetime value in UTC:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select cast(sys_extract_utc(from_tz(cast(dte as timestamp), 'America/Los_Angeles')) as date) from data
CAST(SYS_EXTRACT_UTC(FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES'))ASDATE)
2019-04-30 19:00:00

ORA-01857: not a valid time zone

I am trying to convert from UTC time zone to GMT time zone.
I ran this below query and getting ORA error.
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual;
And error is
Error starting at line : 1 in command -
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual
Error report -
ORA-01857: not a valid time zone
I googled and find that NEW_TIME function is not accepting UTC time zone.
So, Can you please suggest me alternate solution/any way to convert from UTC to GMT?
UTC is also known as GMT, the latter which NEW_TIME already accepts. So, what you are trying to is equivalent to:
SELECT NEW_TIME(SYSDATE, 'GMT', 'GMT')
FROM dual;
The call to NEW_TIME doesn't make any sense of course. Check here for a list of accepted timezone codes.
Use FROM_TZ from convert a timestamp without a time zone to a timestamp with time zone (i.e. UTC) and then use AT TIME ZONE 'GMT' to convert it from the first time zone to the GMT time zone. You'll need to use CAST in various places as FROM_TZ expects a TIMESTAMP rather than a DATE and then you need to cast back to a DATE at the end (assuming you don't want a TIMESTAMP value):
SELECT CAST(
FROM_TZ(
CAST( SYSDATE AS TIMESTAMP ),
'UTC'
)
AT TIME ZONE 'GMT'
AS DATE
) As gmt_time
FROM DUAL
Output:
| GMT_TIME |
| :------------------ |
| 2019-04-10T14:05:37 |
db<>fiddle here

date/time Conversion between different timezones

I am using the following sql to covert a date/time value from one timezone to another
from_tz(cast(to_date(to_char(q.created_date, 'DDMMYYYY:HH24:MI:SS'),
'DDMMYYYY:HH24:MI:SS') as timestamp), 'Europe/London') at time zone 'America/New_York'
else null end as Message_Rcd_Date_Time
output from the above is as follows:
29-OCT-2016 14:28:16.000000 -04:00
What I want to do is output this date/time as shown below, excluding the timezone, can you please tell me what I need to change in order to achieve this?
29-OCT-2016 14:28:16
First let's dissolve your expression
FROM_TZ(CAST(TO_DATE(TO_CHAR(q.created_date, 'DDMMYYYY:HH24:MI:SS'), 'DDMMYYYY:HH24:MI:SS') AS TIMESTAMP), 'Europe/London') AT TIME ZONE 'America/New_York'
does following:
TO_CHAR(q.created_date, 'DDMMYYYY:HH24:MI:SS') -> Convert created_date value to VARCHAR2
TO_DATE(..., 'DDMMYYYY:HH24:MI:SS') -> Convert it back to a DATE
CAST(... AS TIMESTAMP) -> Convert it to a TIMESTAMP (without time zone)
FROM_TZ(..., 'Europe/London') -> Attach time zone 'Europe/London' to it
... AT TIME ZONE 'America/New_York' -> Convert to time zone 'America/New_York'
Point 1,2 and 3 are useless! Since created_date is a TIMESTAMP you can do it shorter
TO_CHAR(FROM_TZ(q.created_date, 'Europe/London') AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI:SS')
In case your SESSIONTIMEZONE is Europe/London you can even make
TO_CHAR(q.created_date AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI:SS')
Not sure about Oracle but below query can help.
SELECT SUBSTR((cast(to_date(to_char(q.created_date, 'DDMMYYYY:HH24:MI:SS'),
'DDMMYYYY:HH24:MI:SS') as timestamp), 'Europe/London') , 1, 20) at time zone 'America/New_York'
else null end as Message_Rcd_Date_Time;
If it would have been SQL Server only LEFT function would have worked.

Converting NUMBER to DATE

I have a numeric field in my Oracle database that represents the date.
I'm not so familiar with Oracle commands.
I was wondering if anyone could provide some guide here.
Thanks.
example: 1435755908 = 7/1/2015 9:05
This is a Unix Timestamp, i.e. the seconds since January 1970, try this formula:
timestamp '1970-01-01 00:00:00' + 1435755908/86400
Since there seems to be a time zone difference:
select date '1970-01-01' + 1435755908/86400 as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 13:05:08
you seem to need to do some time zone manipulation. Epoch times are UTC so you can use from_tz to declare that, and then at time zone to get the US/East Coast equivalent:
select from_tz(cast(date '1970-01-01' + 1435755908/86400 as timestamp), 'UTC')
at time zone 'America/New_York' as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 09:05:08.000 AMERICA/NEW_YORK
Which is a time stamp with time zone. If you want it as a plain date then cast it:
select cast(from_tz(cast(date '1970-01-01' + 1435755908/86400 as timestamp), 'UTC')
at time zone 'America/New_York' as date) as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 09:05:08

How can I convert time to a number format in oracle for a specific timezone

My database shows the time in GMT.
I am able to convert the hour and minute part of it in number by doing:
TO_NUMBER(TO_CHAR(CORE_UPDATE_TS, 'HH24MI'))
where core_update_ts is the value received from database. However, my requirement is to convert the core_update_ts into America/Los_Angeles and then change it to Number.
I am not able to do this specific task. When I do,
to_number(to_char((from_tz(to_timestamp(to_char(CORE_UPDATE_TS, 'YYYY-MM-DD HH24:MI'),
'YYYY-MM-DD HH24:MI') ,'GMT') at time zone 'America/Los_Angeles'),'DD-MON-YYYY
HH24:MI'), 'HH24MI') i am getting error.
How can I do this?
What is the datatype of CORE_UPDATE_TS column?
If it is date, you should cast it to timestamp and simply specify the timezone, and then extract.
cast(CORE_UPDATE_TS as timestamp) at time zone 'America/Los_Angeles'
to_char(cast(CORE_UPDATE_TS as timestamp) at time zone 'America/Los_Angeles','HH24MI')
If it is timestamp, simply specify the timezone.
CORE_UPDATE_TS at time zone 'America/Los_Angeles'
to_char(CORE_UPDATE_TS at time zone 'America/Los_Angeles','HH24MI')