Skip to Main Content
  • Questions
  • Need help to select dates in Israel time from a database server located in US

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Souvik.

Asked: March 06, 2018 - 10:41 am UTC

Last updated: March 06, 2018 - 2:45 pm UTC

Version: 12.1.0.2.0

Viewed 1000+ times

You Asked

select example_dt from tab where tabid='123'--example_dt is a date column in the table 'tab'

In this above query how can I get/convert example_dt in Israel time if I am running this query from my US database server?

and Chris said...

It doesn't matter where you locate your servers. What matters is which time zone you stored the dates in.

You can use the "at time zone" clause to convert a timestamp from one time zone to another. But first you need to know the original time zone!

If you stored data in a date or timestamp without time zone, you need to convert to this first. Then apply the time zone conversion:

create table t (
  dt date
);

insert into t values (date'2018-01-01');

select dt,
       from_tz ( 
         to_timestamp ( 
           to_char ( dt, 'yyyy-mm-dd hh24:mi:ss' ), 
           'yyyy-mm-dd hh24:mi:ss'
         ), 
         tz_offset('US/Pacific') 
       ) pacific_timezone,
       from_tz ( 
         to_timestamp ( 
           to_char ( dt, 'yyyy-mm-dd hh24:mi:ss' ), 
           'yyyy-mm-dd hh24:mi:ss'
         ), 
         tz_offset('US/Pacific') 
       ) at time zone 'Asia/Tel_Aviv' israel_time
from   t;

DT                     PACIFIC_TIMEZONE           ISRAEL_TIME                
01-JAN-2018 00:00:00   01-JAN-2018 00.00.00 -08   01-JAN-2018 10.00.00 +02


If you've stored data using "timestamp with time zone", then you already know the source offset. So you can go straight to using "at time zone":

drop table t purge;
create table t (
  ts timestamp with time zone
);

insert into t values (timestamp'2018-01-01 00:00:00 -8:00');

select ts pacific_time, ts at time zone 'Asia/Tel_Aviv' israel_time 
from   t;

PACIFIC_TIME               ISRAEL_TIME                
01-JAN-2018 00.00.00 -08   01-JAN-2018 10.00.00 +02 


But if you've stored the values in a "timestamp with local time zone", the database normalizes the dates to the DBTIMEZONE. You can use "at time zone" with this too. Or, when you fetch them it can convert to the sessiontimezone:

alter session set time_zone = 'Europe/London';

select dbtimezone, sessiontimezone from dual;

drop table t purge;
create table t (
  tsltz timestamp with local time zone
);

insert into t values (
  timestamp'2018-01-01 00:00:00 -8:00'
);

select tsltz at time zone 'Asia/Tel_Aviv' israel_time from t;

ISRAEL_TIME
---------------------------------------------------------------------------
2018-JAN-01 10:00:00 +02:00

select t.*, sessiontimezone from t;

TSLTZ                          SESSIONTIMEZONE
------------------------------ --------------------
01-JAN-18 08.00.00.000000      Europe/London

alter session set time_zone = 'US/Pacific';

select t.*, sessiontimezone from t;

TSLTZ                          SESSIONTIMEZONE
------------------------------ --------------------
01-JAN-18 00.00.00.000000      US/Pacific

alter session set time_zone = 'Asia/Tel_Aviv';

select t.*, sessiontimezone from t;

TSLTZ                          SESSIONTIMEZONE
------------------------------ --------------------
01-JAN-18 10.00.00.000000      Asia/Tel_Aviv

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank you so much.

A reader, March 06, 2018 - 2:53 pm UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library