In PostgreSQL, the minus operator “-” and the AGE() function retrieve the difference between two dates. The “-” operator returns an integer that represents the date difference in days while the AGE() function retrieves the date difference as an interval. The “-” function is useful when you have to find the number of days between two dates while the AGE() function is used to present the date difference with great detail.
This post demonstrates how to find date differences in Postgres using the AGE() function and the minus “-” operator.
How to Find the Date Difference in Postgres Using Minus Operator?
To find the date difference in days, all you need to do is subtract the first date from the second date:
DATE 'date_2' - DATE 'date_1';
Let’s understand it with an example.
Example 1: Finding Date Difference Using “-”
In the below-provided code, the minus operator is used to get the date difference in days:
SELECT DATE '2002-06-01' - DATE '2001-01-01' AS date_diff;
The output shows the difference between given dates in “516” days.
Example 2: Finding the Date Difference From the Current Date Using “-”
The following snippet calculates the difference in days between the current date and the specified date:
SELECT CURRENT_DATE - DATE '2020-01-01' AS date_diff;
The output authenticates that the difference between the current date and the specified date is “1153” dates.
Example 3: Finding the Date Difference in Days From a Table
A sample table named “emp_data” is created in the database with the following data:
Suppose we want to subtract the “joining_date” from the current_date to get the date difference in days. To fulfill this task, we will utilize the minus operator as follows:
SELECT emp_name, joining_date, CURRENT_DATE - joining_date AS day_diff FROM emp_data;
The “-” operator retrieves the date difference in days.
How to Find the Date Difference in Postgres Using AGE() Function?
Specifying the dates to the AGE() function as arguments will retrieve the date difference as an INTERVAL:
AGE('date_1', 'date_2');
Let’s understand the AGE() function using the following examples.
Example 1: Finding the Difference Between Two Dates Using AGE()
In the below code, the AGE() function is used to get the date difference as an interval:
SELECT AGE('2002-06-01', '2001-01-01') AS date_diff;
The output shows that the difference between the given dates is “1 year and 5 months”.
Example 2: Finding the Difference Between From Current Date Using AGE() Function
In the following snippet, the AGE() function is used to get the difference between the current date and the employee’s joining_date:
SELECT emp_name, joining_date, AGE(CURRENT_DATE, joining_date) FROM emp_data;
The AGE() function retrieves the date difference as an interval.
Conclusion
In PostgreSQL, the minus operator “-” and the AGE() function are used to find the difference between two dates. The “-” operator returns an integer while the AGE() function retrieves the date difference as an interval. The “-” function is useful when you have to find the number of days between two dates while the AGE() function is used to present the date difference with great detail. This write-up demonstrated a detailed guide to finding the date difference using the “-” operator and the AGE() function.