In order to add hours to datetime(timestamp) in PostgreSQL we will using the approach by using the + operator, make_interval() and Interval. datatype. Here are examples of the approach. Also, we will look at how to Add hours to datetime(timestamp) column in PostgreSQL table.
Add hours to date in PostgreSQL using INTERVAL data type:
We will be Using the INTERVAL data type to add hours to datetime in PostgreSQL. When we add hours to datetime(timestamp) using INTERVAL datatype the output will be in datetime(timestamp) format. In below example, we have added 1 hour to datetime using Interval data type.
Example 1:
SELECT '2024-02-24 12:00:00'::timestamp + INTERVAL '1 hours' AS new_datetime
Output:
Add hours to datetime in PostgreSQL using make_interval() function:
We will be Using the make_interval() function to add hours to datetime in PostgreSQL. When we add hours to datetime using the make_interval() function, the output will be in datetime(timestamp) format. In below example, we have added 1 hour to timestamp
Example 1:
SELECT '2024-02-24 12:00:00'::timestamp + make_interval(hours => 1) as new_datetime
Output:
Add hours 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 hours to datetime(timestamp) column in postgresql table.
student_detail1:
We have added 2 hours to date column using INTERVAL datatype in postgresql and new column named new_date is being created as shown below.
select *, birthdaytime::timestamp + INTERVAL '2 HOURS' as new_date from student_detail1
Output:
Add hours to datetime column in PostgreSQL table using make_interval() function:
We will have added 2 hours 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(hours => 2) as new_date from student_detail1
Output: