Question

We have a huge Oracle database that contains data which has date and time stamps. Right now we are using mm-dd-yyyy format for date and EST Time zone for time stamp.

Now we need to change the date format as yyyy-mm-dd and time stamps should be GMT timezone. We need to change already existing data in the tables to this new format as well as newly inserted data.

Any help on how to achieve this?

Was it helpful?

Solution

Changing the date format is just a matter of presentation. When selecting from you database, apply a format:

select to_char(sysdate, 'YYYY-MM-DD') from dual;

To get this format by default, set it in your session’s NLS_DATE_FORMAT parameter:

alter session set NLS_DATE_FORMAT = 'YYYY-MM-DD';

You can also set the NLS_TIMESTAMP_FORMAT and NLS_TIMESTAMP_TZ_FORMAT. See Setting Up a Globalization Support Environment in Oracle’s documentation.

Regarding time zones, normally it is the application that inserts the timestamp with time zone in the database that chooses what time zone it puts. But the application that queries these timestamps can request to get them back in another time zone. Compare the following queries:

select systimestamp from dual; -- This gives you the timestamp in the server configured time zone
select systimestamp at time zone 'EST' from dual; -- This is in your EST time zone
select systimestamp at time zone '+00:00' from dual; -- Finally the same timestamp in UTC.

To force a certain time zone you can force it with an after insert or update trigger. Something like:

create or replace trigger my_table_force_utc_timestamps
before insert or update on my_table
for each row
begin
    :new.my_timestamp_with_tz := :new.my_timestamp_with_tz at time zone '+00:00';
end;
/

If you really want to convert your timestamps to UTC you can try:

update my_table set my_timestamp_with_tz = my_timestamp_with_tz at time zone '+00:00';

Edit: to change sysdate time zone before inserting into tables you must in fact use systimestamp at time zone … that will be cast to a date value:

insert into my_table (date_column) values (systimestamp at time zone '+00:00');

You can read more about time zones in Oracle’s documentation Datetime and Time Zone Parameters and Environment Variables. With the example table_dt this would give:

SQL> ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';

Session modifiee.

SQL> CREATE TABLE table_dt (c_id NUMBER, c_dt DATE);

Table creee.

SQL> insert into table_dt values (1, systimestamp);

1 ligne creee.

SQL> insert into table_dt values (2, systimestamp at time zone '+00:00');

1 ligne creee.

SQL> select * from table_dt;

      C_ID C_DT
---------- ----------------------
         1 13-JANV.-2013 16:31:41
         2 13-JANV.-2013 15:32:08

OTHER TIPS

You can use new_time oracle function

SELECT to_char(new_time(to_date('01-03-2013','mm-dd-yyyy HH24:MI'), 
                        'EST', 'GMT'),'yyyy-mm-dd HH24:MI') d
FROM DUAL;

Use this to test on your database:

SELECT to_char(new_time(sysdate, 'EST', 'GMT'),'yyyy-mm-dd HH24:MI') d
FROM DUAL;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top