Add Seconds to datetime(timestamp) in PostgreSQL

In order to add seconds to datetime(timestamp) in PostgreSQL we will be using the make_interval() function and another approach by using the + operator and  Interval. datatype.  datatype. Here are examples of the approach. Also, we will look at how to Add seconds to datetime(timestamp) column in PostgreSQL table.

 

Add seconds to datetime in PostgreSQL using INTERVAL data type:

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

Example 1:

 

SELECT '2024-02-24 12:00:00'::timestamp + INTERVAL '30 seconds' AS new_datetime

Output:

Add Seconds to datetime(timestamp) in PostgreSQL 1           

 

Add Seconds to datetime in PostgreSQL using make_interval() function:

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

Example 1:

 SELECT '2024-02-24 12:00:00'::timestamp + make_interval(secs => 30) as new_datetime 

Output:

Add Seconds to datetime(timestamp) in PostgreSQL 1

 

 

Add seconds to datetime(timestamp) column in PostgreSQL table using Interval datatype:

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

student_detail1:

Add Seconds to datetime(timestamp) in PostgreSQL 2

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

 

select *, birthdaytime::timestamp + INTERVAL '30 SECONDS' as new_date from student_detail1

Output:

Add Seconds to datetime(timestamp) in PostgreSQL 3

 

 

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

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

 

select *,birthdaytime::timestamp + make_interval(secs => 30) as new_date
from student_detail1

Output:

Add Seconds to 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