invalid input syntax for type timestamp

徘徊边缘 提交于 2019-12-13 02:56:33

问题


While running the below code i get an error saying invalid input syntax for type timestamp from 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

回答1:


See 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).

So you can not compare it with an empty string:

date_trunc('day', admission_datetime) = ''

The invalid input syntax for type timestamp error message concerns the empty string (''), not the admission_datetime column.

Furthermore, there is no date_diff function in PostgreSQL. Just subtract one timestamp from another and you will get an interval result:

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

You'll get

interval '1 day 15:00:00'

If you need the difference in days, try this:

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

The result is 1.

See here for examples of DATEDIFF-like expressions in PostgreSQL.



来源:https://stackoverflow.com/questions/50598266/invalid-input-syntax-for-type-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!