我們有一個巨大的Oracle數據庫,其中包含具有日期和時間戳的數據。 現在我們正在使用mm-dd-yyyy format for date and EST Time zone for time stamp
。更改表中數據的日期和時間戳記格式
現在我們需要更改date format as yyyy-mm-dd and time stamps should be GMT timezone
。 我們需要將表格中已有的數據更改爲新格式以及新插入的數據。
如何實現這一目標的任何幫助?
我們有一個巨大的Oracle數據庫,其中包含具有日期和時間戳的數據。 現在我們正在使用mm-dd-yyyy format for date and EST Time zone for time stamp
。更改表中數據的日期和時間戳記格式
現在我們需要更改date format as yyyy-mm-dd and time stamps should be GMT timezone
。 我們需要將表格中已有的數據更改爲新格式以及新插入的數據。
如何實現這一目標的任何幫助?
更改日期格式是只是呈現的問題。從你的數據庫中進行選擇時,應用格式:
select to_char(sysdate, 'YYYY-MM-DD') from dual;
默認情況下要獲得這種格式,將它設置在會話的NLS_DATE_FORMAT
參數:
alter session set NLS_DATE_FORMAT = 'YYYY-MM-DD';
您還可以設置NLS_TIMESTAMP_FORMAT
和NLS_TIMESTAMP_TZ_FORMAT
。請參閱Oracle文檔中的Setting Up a Globalization Support Environment。
關於時區,通常是將timestamp with time zone
插入數據庫的應用程序,該數據庫選擇它放置的時區。但是查詢這些時間戳的應用程序可以請求將它們返回到另一個時區。比較以下查詢:
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.
迫使你可以用after insert or update
觸發強制其一定的時區。喜歡的東西:
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;
/
如果你真的要轉換的時間戳爲UTC你可以嘗試:
update my_table set my_timestamp_with_tz = my_timestamp_with_tz at time zone '+00:00';
編輯:以插入您必須在事實表之前改變sysdate
時區使用將被投射到日期值的systimestamp at time zone …
:
insert into my_table (date_column) values (systimestamp at time zone '+00:00');
您可以在Oracle文檔Datetime and Time Zone Parameters and Environment Variables中閱讀有關時區的更多信息。隨着例子table_dt
這將給:
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
可以使用new_time Oracle函數
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;
使用此來測試你的數據庫上:
SELECT to_char(new_time(sysdate, 'EST', 'GMT'),'yyyy-mm-dd HH24:MI') d
FROM DUAL;
我的問題是更類似於http://stackoverflow.com/questions/11106703/change-timezone-component-of-timestamp-with-timezone-in-oracle。另外我還需要日期格式更改。 –
正如我在回答最近的另一個問題時所解釋的那樣,日期格式是*顯示屬性*,並且與日期的存儲方式無關:http://stackoverflow.com/a/14144928/146325 – APC
@APC是爲'日期'。但'帶時區的時間戳'時區信息實際上存儲在數據庫中。看到我的答案。 – kmkaplan