类型时间戳的无效输入语法

invalid input syntax for type timestamp

虽然 运行 下面的代码我收到一条错误消息,指出来自 admission_datetime 的时间戳类型的输入语法无效。

    UPDATE ccsm.stg_demographics_baseline
    SET xx_los_days =
    (CASE WHEN admission_datetime IS NULL OR 
    date_trunc('day',admission_datetime) = ''
    THEN NULL
    WHEN discharge_datetime IS NULL OR 
    date_trunc('day',discharge_datetime) = ''
    THEN date_diff('day', admission_datetime, CURRENT_DATE)
    ELSE
    date_diff('day', admission_datetime, discharge_datetime)
    END);
enter code here

date_trunc documentation:

The return value is of type timestamp or interval with all fields that are less significant than the selected one set to zero (or one, for day and month).

所以你不能将它与空字符串进行比较:

date_trunc('day', admission_datetime) = ''

invalid input syntax for type timestamp 错误消息涉及空字符串 (''),而不是 admission_datetime 列。

此外,PostgreSQL中没有date_diff函数。只需从另一个中减去一个 timestamp,您将得到一个 interval 结果:

SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'

你会得到

interval '1 day 15:00:00'

如果您需要的差异,试试这个:

SELECT DATE_PART('day', timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00')

结果是1.

有关 PostgreSQL 中类似 DATEDIFF 表达式的示例,请参阅 here