![]() The above query will retrieve an integer value representing the total number of days between the specified dates.Įxample 1: How to Get the Interval Between Two Dates Using “-” Operator?Įxecute the following query to get the number of days between the given dates: SELECT ''::DATE - ''::DATE Use the following syntax to get the interval via the “ -” operator: 'date_1'::DATE - 'date_2'::DATE In PostgreSQL, the minus “ -” operator is used to get an interval between the two dates. ![]() How to Get an Interval Between Two Date Fields Via the “-” Operator in Postgres? This way, you can find the interval between two dates via the Postgres’ AGE() function. Let’s find the total work experience of each individual via the AGE() function: SELECT emp_name, AGE(emp_resign_date, emp_join_date) AS total_experience We have already created a table named “emp_example” that contains the necessary data of all the company’s employees: SELECT * FROM emp_example The output shows that the AGE() function calculates the age of the selected person and retrieves an INTERVAL as output.Įxample 2: How to Find Employees’ Work Experience Using the AGE() Function in Postgres? Consequently, the AGE() function will retrieve the following output: In the above snippet, we pass today’s date as the first argument and the person’s birth date as the second argument. To do that, we will execute the AGE() function as follows: SELECT AGE(CURRENT_DATE, '') Let’s suppose we want to calculate the age of a person whose birth date is “”. The above snippet shows that the AGE() function accepts two timestamps/dates as arguments.Įxample 1: How to Calculate the Person’s Age Using AGE() Function in Postgres? Use the following syntax to get the services of the Postgres AGE() function: AGE(TIMESTAMP_1, TIMESTAMP_2) It subtracts the second timestamp/date field from the first and retrieves the resultant INTERVAL as output. In Postgres, AGE() is a built-in function that accepts two dates as arguments and finds the interval between the given dates. How to Find/Get the Interval Between Two Date Fields Via AGE() Function in Postgres? This tutorial will explore the usage of the Postgres’ AGE() function and the minus “ -” operator via practical examples. Both these approaches are useful for calculating the person's age, finding the duration of an event, analyzing job experience, and so on. Finding the interval between two dates in PostgreSQL is a common task that can be accomplished using the built-in AGE() function or the minus “-” operator. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |