【问题标题】:invalid input syntax for type interval: "missing"类型间隔的无效输入语法:“缺失”
【发布时间】:2015-12-20 07:33:27
【问题描述】:

我正在使用 PostgreSQL 8.4.4。我的问题是,我正在从两个 bigint 值计算以天-小时-分钟为单位的时间差。我将这些值转换为时间戳,然后计算差异。我得到了表中存在的值的差异。但对于那些返回NULL 或表中不存在的人,我希望结果为“缺失”。当我在查询中添加它时,我得到了上述错误。以下是我正在使用的查询:

select 
   case when exists (SELECT age(to_timestamp(incepted_date), to_timestamp(invoice_pay_date)) 
                    from transactions_transactions where id = 4275) 
   then (SELECT age(to_timestamp(incepted_date), to_timestamp(invoice_pay_date)) 
                    from transactions_transactions where id = 4275)   
   else 'missing'
end

【问题讨论】:

  • 试试这个select coalesce(nullif(age(to_timestamp(incepted_date), to_timestamp(invoice_pay_date))::text,''),'Missing') FROM transactions_transactions WHERE id = 4275或者这个-hastebin.com/iyaxavilef.sql
  • 非常感谢@winged panther 的快速响应,解决方案按预期工作。

标签: postgresql select timestamp bigint


【解决方案1】:

您可以使用coalesce()nullif() 函数的组合来实现结果,而不是像下面那样使用CASE-WHEN

SELECT coalesce(nullif(age(to_timestamp(incepted_date), to_timestamp(invoice_pay_date))::TEXT, ''), 'Missing')
FROM transactions_transactions
WHERE id = 4275

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2019-02-11
    • 2018-05-22
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多