2017-06-17 101 views
0

我想改變列「yearcode」的日期格式爲四位數(yyyy),因爲我沒有具體的一天數據。改變數據格式使用to_date不會改變位數(postgresql)

但是,當我檢查列的數據格式時,表格仍以「yyyy-mm-dd」格式顯示日期格式。

這裏是我爲了寫了轉換格式的代碼:

alter table postmaster_compensation alter yearcode type date using to_date('1845','yyyy') 

我不知道是否有與代碼的任何問題,或者我不能在所有更改格式爲「YYYY」模式? 9.6引用說'yyyy'是有效的日期格式。

謝謝!

+0

'TO_DATE( '1845', 'YYYY')'賦予 「1845年1月1日」,而不是 「1845」 –

+0

我明白了,謝謝! – Bec

回答

0

如果列應該是焦炭,然後更改列的格式爲char和使用TO_CHAR

alter table postmaster_compensation alter yearcode type char(4) using to_char('01/01/1845','yyyy') 

拿到年終如果列應該是整數,那麼更改列的格式爲int並提取一年之日起

alter table postmaster_compensation alter yearcode type int using extract(year from '01/01/1845','yyyy')