首页 > 解决方案 > 如何使用 TIMESTAMPDIFF 获取 PostgreSQL 中 2 个日期之间的差异?

问题描述

我试图得到它:

SELECT * FROM "Employee" WHERE TIMESTAMPDIFF('YEAR', "BirthDate", "HireDate");

但我得到下一个错误:

ERROR:  function timestampdiff(unknown, timestamp without time zone, timestamp without time zone) does not exist
LINE 1: SELECT * FROM "Employee" WHERE TIMESTAMPDIFF('YEAR', "BirthD...
                                   ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

我找到了另一种方法来获得两个日期之间的差异,但在我的说明中它说我必须通过TIMESTAMPDIFF. 有人可以帮我看哪里以及如何解决我的错误吗?

标签: sqlpostgresql

解决方案


您可以使用减号运算符

EXTRACT(DAY FROM (HireDate)-(BirthDate)) AS DateDifference

例子select date '2001-10-01' - date '2001-09-28'

小提琴演示

PostGrey 文档


推荐阅读