【问题标题】:Convert "time with time zone" to "timestamp with time zone" in Postgresql在 Postgresql 中将“带时区的时间”转换为“带时区的时间戳”
【发布时间】:2014-09-11 16:48:34
【问题描述】:

我必须将表格列从“带时区的时间”更改为“带时区的时间戳”。

我试过了

alter table mytable
alter column date type timestamp with time zone using date::timestamp with time zone

但我收到一个错误,即 postgres 无法自动转换这些类型。 我怎样才能做到这一点? SQLFiddle

【问题讨论】:

标签: postgresql


【解决方案1】:

您可以指定using null 作为以空值开头的转换机制:

alter table mytable
  alter column date type timestamp with time zone
  using null;

【讨论】:

  • 所以我必须先截断我的表?有没有办法隐藏此列?
  • @0bmis:不需要截断行,但是对于转换,您必须提供自己的方法。时间不包含足够的信息来制作时间戳(它缺少日期),并且 postgres 无法发明该信息。
【解决方案2】:

@Winged Panther 非常感谢你 ;) 因为时间泄露了日期,所以可以用一个固定的日期进行转换:

alter table mytable
alter column date type timestamp with time zone using date('20140101') + date;

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2023-03-28
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2019-10-07
    • 2015-10-07
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多