How to get the duration with postgresql?

Member

by orpha , in category: MySQL , 13 days ago

How to get the duration with postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 12 days ago

@orpha 

To get the duration in PostgreSQL, you can use the age() function along with the NOW() function to calculate the difference between two timestamps. Here's an example:

1
SELECT age(NOW(), '2022-01-01 00:00:00') AS duration;


This query will calculate the duration between the current timestamp and January 1, 2022. The result will be displayed in the format of years, months, days, hours, minutes, and seconds.


You can also use the date_trunc() function to get a more precise duration. For example, to get the duration in hours, you can use the following query:

1
SELECT date_trunc('hour', age(NOW(), '2022-01-01 00:00:00')) AS duration_hours;


This query will return the duration in hours between the current timestamp and January 1, 2022. You can change the hour parameter in the date_trunc() function to get the duration in a different unit (e.g., minutes, seconds, days, etc.).


I hope this helps! Let me know if you have any further questions.