Skip to Main Content
  • Questions
  • alter database set time_zone doesn't work

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Jessica.

Asked: March 17, 2002 - 2:53 pm UTC

Last updated: January 18, 2010 - 12:39 pm UTC

Version: 9.0.1

Viewed 50K+ times! This question is

You Asked

Hi Tom,
We have a Oralce 9i database run on Sun Solaris Server, the Unix server timezone is set to 'EST'(Eastern Standard Time). When we created the database, the db timezone wasn't explicitly specified. So when I issued the following statement:
"Select dbtimezone from dual"
I got '-05:00' from sqlplus.It shows absolute time zone offsets from GMT. This is expectable since the db timezone defaults to Unix Server timezone. But what if I want the above select statement returns time zone regions code(like 'EST') instead?
By looking Doc ID: Note:149120.1 in Metalink, I issued the following statement:

ALTER DATABASE <my db name> SET TIME_ZONE = 'US/Eastern'
or
ALTER DATABASE <my db name> SET TIME_ZONE = 'EST'

But when I issued "SELECT DBTIMEZONE FROM dual;", It still show the offsets.
I tried another thing: I set nls_time_tz_format='EST' in the init.ora file and restart the database. After I issued "SELECT DBTIMEZONE FROM dual;" This time, it shows 'EST'. But still, "alter database..." couldn't change anything, it stick to 'EST' from then on...

One word, it seems the alter database statement doesn't work. Following is a quotes from the document from Metalink:

"If the database time zone is set to ’US/Pacific’, then it will return ’US/Pacific’. However, if the database time zone is set to the absolute time zone offset, such as ’-07:00’, it returns ’-07:00’ since it may not map to a unique time zone region considering that daylight savings may be in effect."

My question is: besides in the create databse statement,how to set database timezone?

Thanks for your help?




and Tom said...

that note is missing "the alter database only takes effect after a shutdown/restart". thats why the init.ora appears to work - the alter database kicked in at that point.



ops$tkyte@ORA9I.WORLD> select dbtimezone from dual;

DBTIME
------
-04:00

ops$tkyte@ORA9I.WORLD> alter database set time_zone = 'EST';

Database altered.

ops$tkyte@ORA9I.WORLD> select dbtimezone from dual;

DBTIME
------
-04:00



ops$tkyte@ORA9I.WORLD> connect / as sysdba;
Connected.
ops$tkyte@ORA9I.WORLD> shutdown
Database closed.
Database dismounted.
ORACLE instance shut down.
ops$tkyte@ORA9I.WORLD> startup
ORACLE instance started.

Total System Global Area 358909124 bytes
Fixed Size 279748 bytes
Variable Size 335544320 bytes
Database Buffers 16777216 bytes
Redo Buffers 6307840 bytes
Database mounted.
Database opened.
ops$tkyte@ORA9I.WORLD> select dbtimezone from dual;

DBTI
----
EST




Rating

  (16 ratings)

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

Comments

Alter database not working?

Sikandar Hayat, July 29, 2003 - 6:59 am UTC

1>
"thats why the init.ora appears to work "
I don't think that alter database will change in init.ora. Is this a control file?

2>
I am trying to change the time zone of my db with alter database but it is giving error?

SQL> select dbtimezone from dual;

DBTIME
------
-07:00

SQL> alter database set time_zone='-05:00';
alter database set time_zone='-05:00'
*
ERROR at line 1:
ORA-02231: missing or invalid option to ALTER DATABASE

while alter session is working fine.
SQL> alter session set time_zone = '-05:00';

Session altered.

SQL>  

Tom Kyte
July 29, 2003 - 7:22 am UTC

1) the alter database didn't change the init.ora.

it was a case of false causality. they said "but we changed the init.ora and restarted and things worked differently. must be the init.ora change". NO, it was the alter database change that took effect due to the restart.




2)
The database time zone is relevant only for TIMESTAMP WITH LOCAL TIME ZONE
columns. Oracle normalizes all TIMESTAMP WITH LOCAL TIME ZONE data to
the time zone of the database. You can set the database time zone when the
database is created.

It is not possible to change the database timezone when TIMESTAMP WITH LOCAL TIME ZONE columns are used. Since TIMESTAMP WITH LOCAL TIME ZONE columns are stored normalized to the database timezone, that means that after changing the database timezone the interpretation of the data would be wrong. Therefore, if there is any TIMESTAMP WITH LOCAL TIME ZONE column in the database and
you try to change the time zone you will receive that error



How do I change just the time?

D'Arcy P, October 28, 2003 - 4:46 pm UTC

The time Oracle is using is 9 minutes fast and we want to change it. I cannot find any reference to changing it on your site or OTN.
In case you need to know, dbms is on a Sun box, client machines are Win2000 pcs.

Sorry for using this thread to ask a new ?, but it's kinda related (!)
Thanks Tom

Tom Kyte
October 28, 2003 - 9:09 pm UTC


umm, the time on the SERVER itself is therefore 9 minutes fast since we get the time from the OS.

use the OS utilities to fix your time.

Timezone

Suraj, April 05, 2004 - 1:10 pm UTC

The explaination given here solved most of my queries regarding timezone. I will change the database timezone and bounce back.
Let see the result.

Thanks a lot for helping us and most important is that you are alway prompt in replying. Keep the work going on....

Still one query is left out.
My requirement is to display the date / or pass the date in the format '05-APR-04 05.06.12.663999 PM GMT'.

How should I get it?

when I query, I am getting as

select systimestamp from dual;

SYSTIMESTAMP
---------------------------------------------------------------------------
05-APR-04 05.06.12.663999 PM +00:00

Please advice.






Tom Kyte
April 05, 2004 - 5:16 pm UTC

you'll need a date that is in that time zone first. +00:00 isn't "gmt" yet.


just like +05:00 isn't EST/EDT/or Indiana. that is an offset from GMT, to get GMT, you have to have some GMT times.

Time_zone

Branka, November 05, 2004 - 3:11 pm UTC

%sqlplus "/as sysdba"

sys@TPROD3> select sessiontimezone from dual;
-05:00

sys@TPROD3> select dbtimezone from dual;
-07:00

How can database time_zone be -7 and for session -5?




Tom Kyte
November 05, 2004 - 5:45 pm UTC

database has a timezone. Maybe the server is in New Mexico

you have a timezone ( from your client ). Maybe you are in New York.



your environment in unix for exmaple or via a pretty gui in windows. you have a timezone (and that drives your "sessions" timezone)

TIME_ZONE

BRANKA, November 08, 2004 - 5:02 pm UTC

Did you ever hear that if you conncet to the database with PERL, you have to run
ALTER SESSION SET TIME_ZONE
from every page on IU?
I just can beleive that DBI must work on that way

Alter session set time_zone

Branka, November 10, 2004 - 11:51 am UTC

Tom,
Can you please help me to understand something? I understood that you would have the session time_zone based on OS from where you are sending request and that you do not have to do ALTER SESSION SET TIME ZONE = -5 every time when you connect to the database.

Am I missing something when I think that sending command “ALTER SESSION SET TIME_ZONE=-5” from every page of the User Interface is unnecessary work?

We use PERL as User Interface, and I noticed that from every page of user interface ALTER SESSION SET TIME_ZONE command is sent. The PERL programmer did some research and told me, that it is coming through at the OCI level and that Oracle requests it to work that way.


Tom Kyte
November 10, 2004 - 12:26 pm UTC

"unnessary"?

depends. say the application is a web based calendar. I use it (east coast), you use it (west coast), database is in australia, application server in japan.

now what - whose timezone would you like to be used when YOU schedule an appointment?



ALTER SESSION SET TIMEZONE

Branka, November 10, 2004 - 1:14 pm UTC

That would have seance if you use timestamp with timezone column, but why to send "alter session .." statement if you do not use timezone?

Tom Kyte
November 10, 2004 - 7:50 pm UTC

how does it know you won't use a timezone.

Time zone

Branka, November 11, 2004 - 12:37 am UTC

Thank you for clarifying things for me.
I did not find in documentation that kind of information. Somehow I expected that programmers have to make that kind of decision for their application, not that decision is already made for them.
I guess that a lot of people would like to know that their web application is already sending ALTER SESSION statement, so they do not have to worry about it.


Timezone

Anil, June 09, 2005 - 1:26 am UTC

Hi Tom

I have my server running with timezone +4.00. Now I want my database to show time in GMT.

ie when server time is 14.00 Hrs and when I give

select to_char(sysdate,'ddmonyyyy hh24:mi:ss') from dual
should give me date with time as 10.00 Hrs.

I cannot change the server time since it has lot of other databases in it.

I want only one database runs in GMT time also I don't want to do alter session every time. When you login to the datavase saves a datetime in a table the time should be in GMT


PLease advice


Tom Kyte
June 09, 2005 - 6:56 am UTC

no version information :(

9i supports a database timezone, you could set the dbtimezone (assuming you don't have any timestamps with local timezones already). you could set the session timezone via a logon trigger.

else, you would have to set the TZ environment variable before starting the database instance but be prepared for inconsistent dates when someone "forgets" to set this.



Time zone

Anil, June 09, 2005 - 1:39 am UTC

Hi Tom

From the documentation it looks like I have to created the datavase with SET time_zone option to set it to GMT.

Am I correct

Rgds
Anil

timezone

anil, June 09, 2005 - 11:32 am UTC

sorry tom for that , My database is 10g


Okay , That means I can set the Env variable ,

What about creating the database with SET time_zone . This will save us from forgetting to set etc...

Rgds
Anil

Tom Kyte
June 09, 2005 - 6:04 pm UTC

you can alter the database IF you don't have any timestamps with local time zone, you need not recreate (that was my first comment above in fact)

Very helpful! Answered my lingering questions.

Bernice, August 27, 2005 - 4:35 pm UTC

Users may also want to look at Note:227334.1 Dates, Timestamps, and Timezones - Frequently Asked Questions from Metalink.


Why doesn't dbtimezone affect sysdate?

R. Vernick, May 06, 2008 - 8:13 pm UTC

There are so many questions surrounding the simple(?) issue of "if I set the database timezone, why doesn't sysdate return the adjusted timezone date/time?

The answer "because we retreive the date/time from the os via the getdatetime function" isn't good enough. In my opinion, it should be effected by the dbtimezome setting. Simple stated, "this database exists in this timezone".

We run applications hosted in one data center (with multiple databases on one server [yes, windows]) which serve locations in different timezones (1 database = 1 location). Our application contains >900 date fields and changing them to timezone types is not possible.

Do you have a suggestion?

Thanks, Tom. I really enjoy and get a TON of useful information here. It's amazing how much material you cover.
Tom Kyte
May 07, 2008 - 1:30 am UTC

sysdate is not timezone aware, sensitive, nothing.

It is the date returned to us by the system - by the operating system.

that answer is in fact "good enough", your opinion is just that really. Mine is too. They are our opinions.

this is the way sysdate has worked since before I was using Oracle. To change the way something that is used by *every* Oracle program ever written would be a disaster.

that is my opinion.

You say "changing my applications to timezone is not possible". Think about what would happen to the entire world if we changed the default behaviour of something so heavily used?????? That, that would be a recoding effort that makes your REQUIREMENT look trivial.

Look, you have a brand new requirement (must be new, cannot be old - else you would have done something about it years ago). New requirements sometimes require......... changes to code.



... Our application contains >900 date fields and changing them to
timezone types is not possible.
......

that statement is entirely and utterly false on the fact of it. Entirely false.


Howerver:

... Our application contains >900 date fields and changing them to meet our requirements is a decision we have made....

is true, that statement is entirely true.


you used a function that is not timezone aware. It has never been time zone aware, it never will be, it would be a disaster if it was made to be (for everyone).


You need not change your 900 columns by the way, you need only to use a function that returns a timezone sensitive date for you - localtimestamp for example (return date in timezone of client session - that way you could actually consolidate your databases since the really big mistake you made here is having more than one database!)

ops$tkyte%ORA10GR2> alter session set time_zone='-04:00';

Session altered.

ops$tkyte%ORA10GR2> select sessiontimezone from dual;

SESSIONTIMEZONE
---------------------------------------------------------------------------
-04:00

ops$tkyte%ORA10GR2> declare
  2     l_dt  date := sysdate;
  3     l_tz  date := localtimestamp;
  4  begin
  5          dbms_output.put_line( 'sysdate = ' || l_dt );
  6          dbms_output.put_line( 'localtimestamp = ' || l_tz );
  7  end;
  8  /
sysdate = 07-may-2008 01:28:33
localtimestamp = 07-may-2008 01:28:33

PL/SQL procedure successfully completed.

ops$tkyte%ORA10GR2> alter session set time_zone='+02:00';

Session altered.

ops$tkyte%ORA10GR2> select sessiontimezone from dual;

SESSIONTIMEZONE
---------------------------------------------------------------------------
+02:00

ops$tkyte%ORA10GR2> declare
  2     l_dt  date := sysdate;
  3     l_tz  date := localtimestamp;
  4  begin
  5          dbms_output.put_line( 'sysdate = ' || l_dt );
  6          dbms_output.put_line( 'localtimestamp = ' || l_tz );
  7  end;
  8  /
sysdate = 07-may-2008 01:28:33
localtimestamp = 07-may-2008 07:28:33

PL/SQL procedure successfully completed.


sysdate and timezone

A reader, November 04, 2008 - 11:26 am UTC

greetings thomas,

and thanks like always.

my case as follow:
1) when i connect like this:
sqlplus /nolog
conn / as sysdba

select sysdate from dual;

SYSDATE
-------------------
04-11-2008 17:56:47

2) when i connect through tnsname:
sqlplus /nolog
connect sys@orcl as sysdba

select sysdate from dual;

SYSDATE
-------------------
04-11-2008 13:56:58

why?

Thankx

Hi Tom,

viji., January 12, 2010 - 1:45 am UTC

oracle's default date format is "dd-mm--yy". i want to change it as "yyyy-mm-dd hh24:mi:ss". So i created a trigger as follows,
CREATE OR REPLACE TRIGGER DATE_TRIG
AFTER LOGON ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
END;

But after inserted the values to the table it shows only
yyyy-mm-dd value. Not the hh-mm-ss value.
Why is that?
i need to insert value in yyyy-mm-dd hh24:mi:ss format. I can not use to_date function. Pls help me on this?
Where i did wrong?

Thanks in advance,
-viji.
Tom Kyte
January 18, 2010 - 12:39 pm UTC

nope, Oracles default since just before the year 2000 has been dd-mon-rr


... I can not use to_date
function....

OH YES YOU CAN, do not ever even think about saying that. You can use the to_date function (it would be the RIGHT way), you have purposely chosen to NOT use to_date (you have made a decision to do it the wrong way).

It is not that you cannot use to_date, you have purposely chosen to NOT use to_date


ops$tkyte%ORA10GR2> CREATE OR REPLACE TRIGGER DATE_TRIG
  2      AFTER LOGON ON DATABASE
  3     BEGIN
  4        EXECUTE IMMEDIATE 'alter session set  nls_date_format="yyyy-mm-dd hh24:mi:ss"';
  5     END;
  6  /

Trigger created.

ops$tkyte%ORA10GR2> create table t ( d date );

Table created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> connect /
Connected.
ops$tkyte%ORA10GR2> select sysdate from dual;

SYSDATE
-------------------
2010-01-18 13:39:00

ops$tkyte%ORA10GR2> insert into t values ( '2010-01-01 12:34:56' );

1 row created.

ops$tkyte%ORA10GR2> select * from t;

D
-------------------
2010-01-01 12:34:56

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> drop trigger date_trig;

Trigger dropped.



but in any case, your hack works, not sure what you mean since you provide NO EXAMPLE...

Difference

Manu Batham, January 24, 2014 - 10:31 pm UTC

Hi,

I am not sure about the usage of dbtimezone.

select dbtimezone from dual;
select systimestamp from dual;

Shows different timezones, why?

Thanks,
Manu