簡體   English   中英

更改表中數據的日期和時間戳格式

[英]Change the date and time stamp format for data in the table

我們有一個龐大的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更改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_FORMATNLS_TIMESTAMP_TZ_FORMAT 請參閱Oracle文檔中的設置全球化支持環境

關於時區,通常是應用程序在數據庫中插入timestamp with time zonetimestamp 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 …使用systimestamp at time zone …它將被systimestamp at time zone …轉換為日期值:

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

您可以在Oracle的文檔日期時間和時區參數和環境變量中閱讀有關時區的更多信息。 使用示例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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM