Subtract seconds from current datetime(timestamp) in PostgreSQL

In order to Subtract Seconds from current datetime(timestamp) in PostgreSQL we will be using the two approaches one by using the make_interval() and  other by Interval. datatype. Here are examples of both the approaches. Also, we will look at how to Subtract Seconds from current datetime(timestamp) column in PostgreSQL table.

 

Subtract Seconds from current timestamp in PostgreSQL using INTERVAL data type:

We will be Using the INTERVAL data type to Subtract Seconds from current datetime in PostgreSQL. When we Subtract Seconds from current datetime(timestamp) using INTERVAL datatype the output will be in datetime(timestamp) format. In below example, we have Subtracted 30 seconds from current datetime using Interval data type.

 

Example 1 using CURRENT_TIMESTAMP:

 

SELECT CURRENT_TIMESTAMP::timestamp - INTERVAL '30 seconds' AS new_datetime

Output:

  Subtract Seconds from datetime(timestamp) in PostgreSQL 1       

 

Example 2 using NOW():

 

SELECT NOW()::timestamp - INTERVAL '30 seconds' AS new_datetime

Output:

Subtract Seconds from datetime(timestamp) in PostgreSQL 1           

 

 

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

We will be Using the make_interval() function to Subtract Seconds from datetime in PostgreSQL. When we Subtract Seconds from datetime using the make_interval() function, the output will be in current datetime(timestamp) format. In below example, we have Subtracted 30 second from timestamp

Example 1 using CURRENT_TIMESTAMP:

 

SELECT CURRENT_TIMESTAMP::timestamp - make_interval(secs => 30) as new_datetime

Output:

Subtract Seconds from datetime(timestamp) in PostgreSQL 1

 

Example 2 using NOW():

 

SELECT NOW()::timestamp - make_interval(secs => 30) as new_datetime

Output:

Subtract Seconds from datetime(timestamp) in PostgreSQL 1

 

 

Subtract Seconds from current datetime(timestamp) column in PostgreSQL table using Interval datatype:

We will be using below fruits table for our example to depict on how to Subtract Seconds from current datetime(timestamp) column in PostgreSQL table.

fruits:

Subtract Seconds from datetime(timestamp) in PostgreSQL 2

We have Subtracted 30 seconds from datetime column using INTERVAL datatype in PostgreSQL and new column named new_date is being created as shown below.

Example 1 : using NOW() function

 

select *,NOW()::timestamp as current_dt, NOW()::timestamp - INTERVAL '30 seconds' as new_date from fruits

 

OR

Example 2 : using CURRENT_TIMESTAMP

 

select *,CURRENT_TIMESTAMP::timestamp as current_dt, CURRENT_TIMESTAMP::timestamp - INTERVAL '30 secondS' as new_date from fruits

Output:

Subtract Seconds from datetime(timestamp) in PostgreSQL 3

 

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

We have Subtracted 30 seconds from datetime column using make_interval() function in PostgreSQL and new column named new_date is being created as shown below.

Example 1 : using NOW() function

 

select *,NOW()::timestamp as current_dt, NOW()::timestamp - make_interval(secs => 30) as new_date from fruits

 

OR

Example 2 : using CURRENT_TIMESTAMP

 

select *,CURRENT_TIMESTAMP::timestamp as current_dt, CURRENT_TIMESTAMP::timestamp -make_interval(secs => 30) as new_date from fruits

 Output:

Subtract Seconds from datetime(timestamp) in PostgreSQL 3

 

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