2017-03-28 53 views
1

我想將文本中的一列數據更改爲時間戳類型。我的數據中沒有時區。我的數據格式是28-03-17 17:22,包括時間和日期,但沒有時區。換句話說,我所有的數據都在同一時區。我該怎麼做?postgreSQL將列數據類型更改爲不帶時區的時間戳

我嘗試了以下多種方法,但我仍然找不到正確的方法。希望您能夠幫助我。

當然,如果我的麻煩可以解決,我可以建立一個新表格。

alter table AB 
alter create_time type TIMESTAMP; 

ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone 
HINT: You might need to specify "USING create_time::timestamp without time zone". 
********** Error ********** 

ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone 
SQL state: 42804 
Hint: You might need to specify "USING create_time::timestamp without time zone". 
alter table AB 
alter create_time type TIMESTAMP without time zone; 

ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone 
HINT: You might need to specify "USING create_time::timestamp without time zone". 
********** Error ********** 

ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone 
SQL state: 42804 
Hint: You might need to specify "USING create_time::timestamp without time zone". 
alter table AB 
alter create_time::without time zone type TIMESTAMP; 

ERROR: syntax error at or near "::" 
LINE 2: alter create_time::without time zone type TIMESTAM 
         ^
********** Error ********** 

ERROR: syntax error at or near "::" 
SQL state: 42601 
Character: 50 
alter table AB 
alter create_time UTC type TIMESTAMP; 

ERROR: syntax error at or near "UTC" 
LINE 2: alter create_time UTC type TIMESTAMP; 
         ^
********** Error ********** 

ERROR: syntax error at or near "UTC" 
SQL state: 42601 
Character: 50 
+1

作爲一般規則,請在您的問題中包含任何相關的錯誤消息。如果沒有錯誤,請提及*爲什麼*您認爲它沒有按預期工作。說這是「不正確的做法」不是很有幫助或者不夠詳細。 –

+0

添加了所有錯誤。請檢查一下。謝謝。 – Amy

+0

閱讀錯誤信息和您的代碼:'使用create_time :: timestamp without time zone'與您的代碼:'create_time :: without time zone type TIMESTAMP;' –

回答

0

如果create_time是有效的日期值類型的文字,這將是更容易的改變進行如下操作(宜首先做一個錶轉儲爲備份):

-- Create a temporary TIMESTAMP column 
ALTER TABLE AB ADD COLUMN create_time_holder TIMESTAMP without time zone NULL; 

-- Copy casted value over to the temporary column 
UPDATE AB SET create_time_holder = create_time::TIMESTAMP; 

-- Modify original column using the temporary column 
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING create_time_holder; 

-- Drop the temporary column (after examining altered column values) 
ALTER TABLE AB DROP COLUMN create_time_holder; 
+0

嗨,獅子座。這是一個正確的方法,設置「set datestyle =」ISO,DMY「。非常感謝你! – Amy

+0

更改格式後,我現有的所有數據都很好。但問題是,當我試圖導入數據錯誤:在最後一個預期的列之後的額外數據 上下文:COPY ca_manifest_checkpoint,第2行:「Canada,YYZ01A,YYZ01A_20170329_151501.txt,953353795858,31,,29/03/2017 14:57:55,30/03/2017 03:20:02,,我......「 – Amy

+0

您是否有機會將表格連同附加支持列一起導出,然後在持有人列之後重新導入下降? –

0

USING...而來的類型後:

... alter create_time type TIMESTAMP USING create_time::TIMESTAMP; 
+0

錯誤:日期/時間字段值超出範圍:「23/03/2017 05:20:02「 建議:也許你需要一個不同的」日期式「設置。 **********錯誤********** 錯誤:日期/時間字段值超出範圍:「23/03/2017 05:20:02」 SQL狀態:22008 提示:也許你需要一個不同的「日期樣式」設置。 – Amy

+0

我很抱歉,它再次顯示錯誤。 – Amy

1

沒有指定原始類型CREATE_TIME的,所以我認爲它是時區與時區(因爲類型DATE或帶有時區的TIMESTAMP在嘗試更改爲不帶時區的TIMESTAMP時不應出現上述錯誤)。由於時間戳記除了時間日期信息,你需要補充你的ALTER語句的最新信息,如:

ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date('20170327') + create_time; 

如果你有相應的DATE列(比如,CREATE_DATE),你可以通過它到date()函數,如:

ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date(create_date) + create_time; 
+0

謝謝你的回答。我設置的數據類型是文本開頭的正弦數據類似'28-03-17 17:22',包括時間和日期,但沒有時區。 – Amy

+0

我發表了我的評論作爲另一個答案 - 評論部分對於顯示多行代碼太有限。 –

-1

從BIGINT改變列的數據類型到時間戳(對於曆元到時間戳)

alter table <tablename> alter column <columnname> type timestamp without time zone using to_timestamp(<columnname>) AT TIME ZONE 'UTC'; 

爲例如;

alter table AB alter column col type timestamp without time zone using to_timestamp(col) AT TIME ZONE 'UTC'; 

從時間戳改變列的數據類型爲bigint(對於時間戳曆元)

alter table <tablename> alter column <columnname> type bigint using extract(EPOCH from <columnname>); 

爲例如;

alter table AB alter column col type bigint using extract(EPOCH from col); 
相關問題