Articles in this section
Category / Section

How to resolve "pg_catalog.pgdate_part" Error When Using DATEDIFF in PostgreSQL

Published:

Introduction

When working with PostgreSQL and attempting to calculate the hour difference between two dates using the DATEDIFF function, you may encounter an error stating that the function pg_catalog.pgdate_part('Unknown', interval) does not exist.

This Knowledge Base article provides a solution to resolve the “pg_catalog.pgdate_part” error and guides you through the steps to calculate the hour difference between two dates.

Issue

When attempting to calculate the hour difference between two dates using DATEDIFF function in PostgreSQL, the function pg_catalog.pgdate_part("Unknown", interval) does not exist error is thrown.

DateDiff.png

Cause

The issue arises when utilizing an invalid or unrecognized date part with the pgdate_part function.

Note: PostgreSQL on Amazon RDS does not have direct support for the DATEDIFF function. We have the custom implementation of this function in Bold BI.

Solution

To calculate the hour difference between two dates without using the DATEDIFF function, follow these steps:

  1. Subtract the start date from the end date.
  2. Extract the epoch (number of seconds) from the resulting interval.
  3. Divide the epoch by 3600 to obtain the hour difference.
Expression: EXTRACT(EPOCH FROM ([enddate] - [startdate])) / 3600

Sample screenshot

Hours Diff Expression.png

Expression Result:

Hours Diff.png

Conclusion

By following the solution provided in this article, you can resolve the pg_catalog.pgdate_part does not exist error when using the DATEDIFF function in PostgreSQL.

Additional References

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
MB
Written by MunishKumar Balakrishnan
Updated
Comments (0)
Please  to leave a comment
Access denied
Access denied