Subtract week from current date in PostgreSQL

In order to subtract week from current date in PostgreSQL we will using three approach one by using the operator, other by using make_interval() and other using the INTERVAL data type. Here are examples of these three approaches. Also, we will look at how to Subtract week from current Date/Time column in PostgreSQL table.

 

Subtract weeks from current datetime in PostgreSQL using INTERVAL data type:

We will be Using the INTERVAL data type to subtract weeks from current date in PostgreSQL. When we subtract weeks from current date using INTERVAL datatype the output will be in datetime(timestamp) format. In below example, we have subtracted 2 weeks from current date using Interval data type.

Example 1:

 

SELECT CURRENT_DATE - INTERVAL '2 WEEKS' as new_date

Output:

Subtract week from current date in PostgreSQL 1

 

Subtract weeks from current date in PostgreSQL using make_interval() function:

We will be Using the make_interval() function to subtract weeks from current date in PostgreSQL. When we subtract weeks from current date using the make_interval() function, the output will be in datetime(timestamp) format. In below example, we have subtracted 2 weeks from current date

Example 1:

 

SELECT CURRENT_DATE::date - make_interval(weeks => 2) as new_date

Output:

Subtract week from current date in PostgreSQL 1

 

 

Subtract weeks from current datetime in PostgreSQL (round about):

To Subtract weeks from current datetime in PostgreSQL we will using CURRENT_DATE keyword with subtraction (-) symbol. Which will subtract 14 days which is 2 weeks from current date as shown below.

Example 1:

 

SELECT  CURRENT_DATE - 14 AS new_date;

Output:

Subtract week from current date in PostgreSQL 2b

 

 

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

We will be using below fruits table for our example to depict on how to subtract weeks from current datetime column in PostgreSQL table.

fruits:

Subtract week from current date in PostgreSQL 3

We have subtracted 2 WEEKS from current date column using INTERVAL datatype in PostgreSQL and new column named new_date is being created as shown below.

 

select *,current_date, Current_date::timestamp  - INTERVAL '2 WEEKS' as new_date from fruits

Output:

Subtract week from current date in PostgreSQL 4

 

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

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

 

select *,current_date, Current_date::timestamp  - make_interval(weeks => 2) as new_date from fruits

 Output:

Subtract week from current date in PostgreSQL 4

 

 

Subtract WEEKS from current date column in PostgreSQL table using Subtraction (-) operator:

In this round about method We have subtracted 14 days which is 2 weeks from current date column using simple subtraction operator (-) in postgresql and new column named new_date is being created as shown below.

 

SELECT *,current_date, current_date-14 as new_date from fruits

Output:

Subtract week from current 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