2017-06-17 48 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')