Add Days to date in PostgreSQL

In order to add days to a date in PostgreSQL we will using two approach one by using the + operator and other using the Interval. datatype. Here are examples of both approaches. Also, we will look at how to Add days to date column in PostgreSQL table.

 

Add days to date in PostgreSQL using simple addition (+) operator:

In the below example we have used simple addition to Add days to date in PostgreSQL. We have added 7 days to date in below example.

Example 1:

 

SELECT '2024-02-24'::date + 7 AS new_date;

Output:

 

 

Add days to date in PostgreSQL using INTERVAL data type:

We will be Using the INTERVAL data type to add days to datetime in PostgreSQL. When we add days to date using INTERVAL datatype the output will be in datetime(timestamp) format. In below example, we have added 7 days to date using Interval data type.

Example 1:

 

SELECT '2024-02-24'::date + INTERVAL '7 DAYS' as new_date

Output:

           

Add days to date in PostgreSQL using make_interval() function:

We will be Using the make_interval() function to add days to date in PostgreSQL. When we add days to datetime using the make_interval() function, the output will be in datetime(timestamp) format. In below example, we have added 7 days to date

Example 1:

 

SELECT '2024-02-24'::date + make_interval(days => 7) as new_date

Output:

           

 

 

 

Add days to datetime  column in PostgreSQL table using Additional (+) operator:

We will be using below student_detail1 table for our example to depict on how to add days to datetime(timestamp) column in postgresql table.

student_detail1:

We will have added 7 days to datetime column using simple addition operator (+) in postgresql and new column named new_date is being created as shown below.

 

SELECT *, birthdaytime::date + 7 as new_date from student_detail1

Output:

 

 

Add days to datetime column in PostgreSQL table using Interval datatype:

We will have added 7 days to datetime column using INTERVAL datatype in postgresql and new column named new_date is being created as shown below.

 

select *, birthdaytime::timestamp + INTERVAL '7 DAYS' as new_date from student_detail1

Output:

 

 

Add days to datetime column in PostgreSQL table using make_interval() function:

We will have added 7 days to datetime column using make_interval() function in postgresql and new column named new_date is being created as shown below.


select *,birthdaytime + make_interval(days => 7) as new_date from student_detail1

Output:

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts