【问题标题】:alter the data format using to_date does not change the number of digits (postgresql)使用 to_date 更改数据格式不会更改位数(postgresql)
【发布时间】:2017-11-20 02:02:45
【问题描述】:

我正在尝试将“年份代码”列的日期格式更改为四位数 (yyyy),因为我没有数据的具体日期。

但是,当我检查该列的数据格式时,表格仍然以“yyyy-mm-dd”格式显示日期格式。

这是我为了更改格式而编写的代码:

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

我想知道代码是否有任何问题,或者我根本无法将格式更改为“yyyy”模式? 9.6 参考说 'yyyy' 是有效的日期格式。

谢谢!

【问题讨论】:

  • to_date('1845','yyyy') 给出“1845-01-01”,而不是“1845”

标签: sql postgresql date format dataformat


【解决方案1】:

如果列应该是char,则将列的格式更改为char并使用to_char获取年份

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

如果该列应为 int,则将列的格式更改为 int 并从日期中提取年份

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2016-01-14
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多