PostgreSQL - AGE Function Last Updated : 01 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In PostgreSQL, the AGE() function is a powerful tool for calculating the difference between two TIMESTAMP values. This function is especially useful for determining the age of individuals or the duration between events. Let us better understand the AGE() Function in PostgreSQL from this article.Syntaxage(timestamp, timestamp);Let's analyze the above syntax:Parameters: The function takes two arguments, both of which are TIMESTAMP values.Operation: It subtracts the second TIMESTAMP from the first one, resulting in an interval that represents the difference between the two timestamps.PostgreSQL AGE Function ExamplesLet us take a look at some of the examples of AGE Function in PostgreSQL to better understand the concept.Example 1: Calculating Age from a BirthdateHere we will evaluate the age of a person whose birth date is '2000-01-01' and the current date '2020-03-20', through the below statement.Query:SELECT current_date, AGE(timestamp '2000-01-01');Output:Explanation: This query will return the current date and the calculated age of the individual based on the birthdate provided. The AGE() function will return an interval showing the number of years, months, and days.Example 2: Determining Rental DurationsThe below statement query to get the top 10 rentals that have the longest durations, from the 'rental' table of the sample database:Query:SELECT rental_id, customer_id, age(return_date, rental_date) AS durationFROM rentalWHERE return_date IS NOT NULLORDER BY duration DESC LIMIT 10;Output:Explanation: This query retrieves the rental ID, customer ID, and the calculated duration for each rental where the return date is available. The results are ordered by duration in descending order, showing the top 10 rentals with the longest durations.Important Points About PostgreSQL AGE FunctionIf only one TIMESTAMP argument is provided, age() calculates the interval from the provided timestamp to the current date and time.If either of the TIMESTAMP values is NULL, the result will be NULL. age() works with TIMESTAMP, DATE, and INTERVAL data types. When using DATE values, they are implicitly cast to TIMESTAMP values with a time component of '00:00:00'.The interval result is approximate when dealing with complex calendar calculations involving leap years or varying month lengths. Comment More infoAdvertise with us Next Article PostgreSQL - AGE Function R RajuKumar19 Follow Improve Article Tags : PostgreSQL PostgreSQL-function PostgreSQL-Date-function Similar Reads PostgreSQL Date Functions PostgreSQL is widely recognized for its comprehensive support for date and time manipulations, making it an excellent choice for applications requiring precise time management and complex calculations. This article explores the core PostgreSQL date functions, covering how to retrieve the current dat 4 min read PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ 6 min read PostgreSQL DATE_PART Function Handling dates and times efficiently is essential for data-driven applications, and PostgreSQL provides powerful built-in functions for managing and manipulating time-based data. One such function is the DATE_PART() function, which allows us to extract specific subfields from date and timestamp valu 5 min read PostgreSQL - EXTRACT Function In PostgreSQL, the EXTRACT() function is a powerful tool used to retrieve specific components of a date or time value. Whether you need to query for a particular year, month, day, or even more detailed time attributes, EXTRACT() can help you extract these fields from date and time values efficiently 2 min read DATEADD() Function in PostgreSQL PostgreSQL is a powerful, open-source relational database system known for its strength and wide range of functionalities. DATEADD function in PostgreSQL adds or subtract time intervals from a given date. In this article, we will discuss basic usage, advanced interval types, and practical examples f 6 min read PostgreSQL - DATE_TRUNC Function The PostgreSQL DATE_TRUNC() function is a powerful tool that helps us truncate a timestamp or interval to a specified level of precision. It is particularly useful when dealing with time-based data for effective data grouping and manipulation, making it ideal for reporting, analytics, and queries wh 4 min read PostgreSQL - LAG Function In PostgreSQL, the LAG() function is a powerful window function that allows you to access data from a previous row within the same result set. Itâs particularly useful for comparing values in the current row with values in the preceding row, making it ideal for analytical queries in PostgreSQL.For e 5 min read PostgreSQL - NOW() Function The NOW() function in PostgreSQL is a powerful and essential tool for retrieving the current date and time. This is particularly useful when recording actions like adding or updating records in our database. Using PostgreSQL NOW() function, we can efficiently track when events occur, making the data 4 min read PostgreSQL - LEAD Function In PostgreSQL, the LEAD() function is a powerful window function used to access a row that follows the current row at a specific physical offset. This function is generally employed to compare the value of the current row with the value of the next row following the current row.Let us better underst 3 min read PostgreSQL - CURRENT_DATE Function The PostgreSQL CURRENT_DATE function is used to retrieve the current date. Itâs a simple and effective way to ensure your database operations are using the correct date, particularly for applications that need timestamp records.Let us better understand the CURRENT_DATE Function in PostgreSQL from th 2 min read Like