Subtract weeks from date in PostgreSQL

In order to Subtract weeks from 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 Subtract weeks from datetime column in PostgreSQL table.

 

Subtract weeks from date in PostgreSQL using INTERVAL data type:

We will be Using the INTERVAL data type to Subtract weeks from date in PostgreSQL. When we Subtract weeks from date using INTERVAL datatype the output will be in datetime(timestamp) format. In below example, we have Subtracted 1 week from date using Interval data type.

Example 1:

 

SELECT '2024-02-24'::date - INTERVAL '1 Weeks' as new_date

Output:

  Subtract weeks from date in PostgreSQL 1         

 

 

Subtract weeks from datetime in PostgreSQL using make_interval() function:

We will be Using the make_interval() function to Subtract weeks from date in PostgreSQL. When we Subtract weeks from date using the make_interval() function, the output will be in datetime(timestamp) format. In below example, we have Subtracted 1 week from date

Example 1:

 

SELECT '2024-02-24'::date - make_interval(weeks => 1) as new_date

Output:

Subtract weeks from date in PostgreSQL 1

 

Subtract weeks from datetime in PostgreSQL using simple subtraction (-) operator (roundabout way):

As mentioned, it’s a roundabout way. In the below example we have used simple subtraction to Subtract weeks from date in PostgreSQL. We have Subtracted 7 days (which is 1 week) from date in below example.

Example 1:

 

SELECT '2024-02-24'::date - 7 as new_date

Output:

Subtract weeks from date in PostgreSQL 2

 

 

 

Subtract weeks from datetime column in PostgreSQL table using Interval datatype:

We will be using below student_detail1 table for our example to depict on how to Subtract weeks from datetime column in postgresql table.

student_detail1:

Subtract weeks from date in PostgreSQL 3

We have Subtracted 2 Weeks from date column using INTERVAL datatype in postgresql and new column named new_date is being created as shown below.

 

select *, birthdaytime::timestamp  - INTERVAL '2 WEEKS' as new_date from student_detail1

Output:

Subtract weeks from date in PostgreSQL 4

 

Subtract Weeks from datetime column in PostgreSQL table using make_interval() function:

We will have Subtracted 2 weeks from date column using make_interval() function in postgresql and new column named new_date is being created as shown below.

 

select *,birthdaytime - make_interval(weeks => 2) as new_date

from student_detail1

Output:

Subtract weeks from date in PostgreSQL 4

 

Subtract Weeks from date column in PostgreSQL table using Subtraction (-) operator (roundabout way) :

Again it’s a roundabout method , We will have Subtracted 14 days (2 weeks) from date column using simple subtraction operator (-) in postgresql and new column named new_date is being created as shown below.

 

SELECT *, birthdaytime::date - 14 as new_date from student_detail1

Output:

Subtract weeks from date in PostgreSQL 5

 

 

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