【问题标题】:Convert varchar dates (BC & AD) to date format in sql在 sql 中将 varchar 日期(BC 和 AD)转换为日期格式
【发布时间】:2016-12-03 18:30:19
【问题描述】:

日期以 varchars 形式存储在另一个表中

select wkdocre from works;
   wkdocre   
-------------
 +1654/12/31
 +1706/12/31
 +1667/12/31
 -0332/12/31
 -0332/12/31
 -1295/12/31

我想将这些日期插入到另一个表中,该表的属性是这样的日期

update ns_works set wor_workcreationdate=(select wkdocre from works where wor_workcreationdate=wkdocre);

我收到此错误

ERROR:  operator does not exist: ns_workcreationdate = dateofcreation
LINE 1: ...lect wkdocre from works where wor_workcreationdate=wkdocre);
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

谢谢

期望的结果

select wor_creationdate from ns_works;
   wor_creationdate   
-------------
 1654/12/31
 1706/12/31
 1667/12/31
 -0332/12/31
 -0332/12/31
 -1295/12/31

【问题讨论】:

  • 编辑您的问题并显示所需的结果。如果你想“插入”一些东西,你为什么要使用update

标签: sql postgresql date varchar


【解决方案1】:

你需要显式转换;试试这样的:

... SET wor_workcreationdate =
        to_date(
           (select wkdocre
            from works
            where wor_workcreationdate = to_date(wkdocre, 'YYYY/MM/DD')),
           'YYYY/MM/DD'
        )

但是,用减号书写公元前年份是不正确的; PostgreSQL 会将-1295 解释为1296 BC,因为0 年实际上是公元前1 年。您可能想要修复您的 works 表并使用 YYYY/MM/DD BC 作为格式说明符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2019-01-23
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多