【问题标题】:Performing a multi-valued update in postgres with dates在带有日期的 postgres 中执行多值更新
【发布时间】:2022-08-11 02:17:43
【问题描述】:

我正在 postgres 中执行多值更新。但是,postgres 中的数据类型 DATE 给我带来了问题。我得到了以下代码来执行更新,但它给出了一个错误

update users as u set
  id = u2.id,
  appointment = u2.appointment
from (values
  (1, \'2022-12-01\'),
  (2, \'2022-12-01\')
) as u2(id, appointment)
where u2.id = u.id;
ERROR:  column \"appointment\" is of type date but expression is of type text
LINE 3:   appointment = u2.appointment
                        ^
HINT:  You will need to rewrite or cast the expression.

通常 postgres 接受这种格式的日期,我应该如何执行此更新?

    标签: postgresql date types


    【解决方案1】:

    Postgres 在创建u2 时首先将日期值转换为字符串,这意味着在执行实际更新时它不能再次将它们转换为日期。将日期转换为解决问题的日期:

    update users as u set
      id = u2.id,
      appointment = u2.appointment
    from (values
      (1, CAST('2022-12-01' as date)),
      (2, CAST('2022-12-01' as date))
    ) as u2(id, appointment)
    where u2.id = u.id;
    

    SQLFiddle:https://dbfiddle.uk/?rdbms=postgres_14&fiddle=9f30e708c8508a2df94458b70399eb3a

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2016-08-24
      相关资源
      最近更新 更多