PostgreSQL中数据类型转换和计算的问题

时间:2018-11-09 07:38:41

标签: postgresql

我的代码如下:

CASE
           WHEN EXTRACT(YEAR
                        FROM REPORTING_DATE) >= EXTRACT(YEAR
                                                              FROM ('2018/11/09 14:11:31' :: DATE)) THEN EXTRACT(YEAR
                                                                                                    FROM REPORTING_DATE)
           WHEN EXTRACT(YEAR
                        FROM ('2018/11/09 14:11:31' :: DATE) - EXTRACT(YEAR
                                                          FROM REPORTING_DATE)) >= 2 THEN EXTRACT(YEAR
                                                                                                        FROM ('2018/11/09 14:11:31' :: DATE))
           WHEN EXTRACT(YEAR
                        FROM REPORTING_DATE) < EXTRACT(YEAR
                                                             FROM ('2018/11/09 14:11:31' :: DATE))
                AND EXTRACT(YEAR
                            FROM REPORTING_DATE) - EXTRACT(YEAR
                                                                 FROM ('2018/11/09 14:11:31' :: DATE)) < 2
                AND EXTRACT(MONTH
                            FROM ('2018/11/09 14:11:31' :: DATE)) < 6 THEN EXTRACT(YEAR
                                                                      FROM REPORTING_DATE)
           ELSE EXTRACT(YEAR
                        FROM ('2018/11/09 14:11:31' :: DATE))

继续操作后,显示消息:

  

运算符不存在:日期-双精度

在该位置:

WHEN EXTRACT(YEAR
                        FROM ('2018/11/09 14:11:31' :: DATE) - EXTRACT(YEAR
                                                          FROM REPORTING_DATE)) >= 2

尽管我已强制转换字符串:

  

2018/11/09 14:11:31->至今类型

和:

  

EXTRACT(YEAR FROM('2018/11/09 14:11:31':: DATE))-> EXTRACT(YEAR FROM ...)->返回双精度值类型

但是它仍然显示该消息。 任何人都可以告诉我我做错了什么。以及我该如何解决。 谢谢。

1 个答案:

答案 0 :(得分:0)

您的括号放在错误的位置。应该是

EXTRACT( YEAR FROM ('2018/11/09 14:11:31' :: DATE)
        ) - EXTRACT(YEAR FROM DATE REPORTING_DATE) >= 2
      --^                                       --^
   --put here                                  --not here