Home » SQL & PL/SQL » SQL & PL/SQL » Convert local time to GMT using off set
Convert local time to GMT using off set [message #650983] Mon, 09 May 2016 07:03 Go to next message
rao_teertum@yahoo.com
Messages: 3
Registered: May 2016
Junior Member
Dear Oracle gurus
I have the requirement to convert local time to GMT
I have local time which is server time
I have offset.

I was trying to understand the following from online help but I ma not sure where I have plugin the offset

select sysdate,
sysdate+(substr(tz_offset(dbtimezone),1,1)||'1')*to_dsinterval('0'||substr(tz_offset( DBTIMEZONE ),2, 5)||':00') from dual;


Thanks in advance for your help



Re: Convert local time to GMT using off set [message #650992 is a reply to message #650983] Mon, 09 May 2016 07:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> col local format a30
SQL> col gmt format a30
SQL> select systimestamp local, systimestamp at time zone 'GMT' gmt from dual;
LOCAL                          GMT
------------------------------ ------------------------------
09/05/2016 14:41:11.062 +02:00 09/05/2016 12:41:11.062 GMT

Re: Convert local time to GMT using off set [message #650993 is a reply to message #650992] Mon, 09 May 2016 07:46 Go to previous messageGo to next message
rao_teertum@yahoo.com
Messages: 3
Registered: May 2016
Junior Member
Michel,
Thank for your reply. I have the column clled offset , I need to use that to calculate the GMT.
Re: Convert local time to GMT using off set [message #650994 is a reply to message #650993] Mon, 09 May 2016 07:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If you want an accurate answer to your problem you must post what it actually is.
So post an example of the data you have.

The generic principles to ask a question are:
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals, as solution depends on it.
With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

[Updated on: Mon, 09 May 2016 07:49]

Report message to a moderator

Re: Convert local time to GMT using off set [message #651013 is a reply to message #650993] Mon, 09 May 2016 16:29 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
There are always various different ways to do things and code that you find on the web may or may not apply to your situation and may or may not be accurate.

If your server uses local time, then you can use the method that Michel showed you to calculate GMT time. You do not need an offset as shown below. You could substitute any timestamp value for systimestamp.
SCOTT@orcl_12.1.0.2.0> col local format a37
SCOTT@orcl_12.1.0.2.0> col gmt	 format a37
SCOTT@orcl_12.1.0.2.0> select systimestamp local,
  2  	    systimestamp at time zone 'GMT' gmt
  3  from   dual;

LOCAL                                 GMT
------------------------------------- -------------------------------------
09-MAY-16 02.16.18.148000 PM -07:00   09-MAY-16 09.16.18.148000 PM GMT

1 row selected.


If you want a date instead of a timestamp, then the following is another method. You could substitute any date value for sysdate.
SCOTT@orcl_12.1.0.2.0> select sysdate local,
  2  	    sysdate - (substr (tz_offset (sessiontimezone), 1, 3) / 24) gmt
  3  from   dual;

LOCAL                                 GMT
------------------------------------- -------------------------------------
09-MAY-2016 02:16:18 PM               09-MAY-2016 09:16:18 PM

1 row selected.


If you have an offset stored in a column in a table in the typical offset format, then you can use something like this if you like. You can substitute any data value for sysdate.
SCOTT@orcl_12.1.0.2.0> create table test_tab (offset  varchar2(6))
  2  /

Table created.

SCOTT@orcl_12.1.0.2.0> insert into test_tab (offset) select tz_offset (sessiontimezone) from dual
  2  /

1 row created.

SCOTT@orcl_12.1.0.2.0> select * from test_tab
  2  /

OFFSET
------
-07:00

1 row selected.

SCOTT@orcl_12.1.0.2.0> select sysdate local,
  2  	    sysdate - (substr (offset, 1, 3) / 24) gmt
  3  from   test_tab
  4  /

LOCAL                                 GMT
------------------------------------- -------------------------------------
09-MAY-2016 02:16:18 PM               09-MAY-2016 09:16:18 PM

1 row selected.


If your offset is stored in some other format, then you need to adapt the code to that format. In the example below, the offset is stored as a numeric value representing the number of hours. You can substitute any date value for sysdate.
SCOTT@orcl_12.1.0.2.0> drop table test_tab
  2  /

Table dropped.

SCOTT@orcl_12.1.0.2.0> create table test_tab (offset  number)
  2  /

Table created.

SCOTT@orcl_12.1.0.2.0> insert into test_tab (offset) select substr (tz_offset (sessiontimezone), 1, 3) from dual
  2  /

1 row created.

SCOTT@orcl_12.1.0.2.0> select * from test_tab
  2  /

    OFFSET
----------
        -7

1 row selected.

SCOTT@orcl_12.1.0.2.0> select sysdate local,
  2  	    sysdate - (offset / 24) gmt
  3  from   test_tab
  4  /

LOCAL                                 GMT
------------------------------------- -------------------------------------
09-MAY-2016 02:16:18 PM               09-MAY-2016 09:16:18 PM

1 row selected.




Re: Convert local time to GMT using off set [message #651014 is a reply to message #651013] Mon, 09 May 2016 17:00 Go to previous message
rao_teertum@yahoo.com
Messages: 3
Registered: May 2016
Junior Member
Barbara,
Thank you very much for detailed answers. That is awesome. That solved my problem.
Previous Topic: Dynamic File Creation using UTL_FILE
Next Topic: VIRTUAL Column - Function
Goto Forum:
  


Current Time: Fri Apr 26 02:24:09 CDT 2024