Subtract one day from a timestamp date in PostgreSQL
Before we begin, let's define what a timestamp is in SQL.
From the PostgreSQL documentation under the DATE/TIME heading, a timestamp is a data type that stores a date and time in the following format.
YYYY-MM-DD hh:mm:ss (DATE | TIME)
The storage size of a timestamp is 8 bytes. It can be displayed with or without a time zone.
The minimum time zone value is 4713 BC and the maximum is 294276 AD.
So now, let's learn how to subtract one day from a timestamp.
INTERVAL
Subtract one day from a timestamp date
using keyword in PostgreSQL
To check the date from a timestamp you can do the following.
select timestamp '2021-01-01 08:08:01'
It will display the timestamp extracted from the string.
You can't do the following to subtract one day from that timestamp. It will throw an error.
select timestamp '2021-01-01 08:08:01' - 1
Output:
ERROR: operator does not exist: timestamp without time zone - integer
LINE 1: select timestamp '2021-01-01 08:08:01' - 1
To solve this problem, we can use INTERVAL
the keyword. What is 间隔
?
INTERVAL
is the number of days of the timestamp, in other words, its age. It does not return a date; rather, the difference in days between two specific dates.
grammar:
age ( timestamp ) ? interval
example:
age(timestamp '1957-06-13') ? 62 years 6 mons 10 days
grammar:
timestamp - timestamp ? interval
example:
timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00' ? 63 days 15:00:00
The syntax provided above is taken from the PostgreSQL documentation.
Use INTERVAL
the value to increase or decrease the number of days from the provided timestamp. Hence, we can use the query given below:
select timestamp '2021-01-01 08:08:01' - INTERVAL '1 DAY'
Doing this will subtract one day from our timestamp.
You can also use the following.
select timestamp '2021-01-01 08:08:01' - INTERVAL '24 HOURS'
Since 24 hours is the same as 1 day, you can use any of the syntax given above.
Performing date conversion in PostgreSQL to subtract one day from a timestamp date
grammar:
select timestamp '2021-01-01 08:08:01'::DATE - 1
This will subtract one day from the date. Because timestamps do not allow subtraction, we can convert it to a date and do the subtraction as needed.
+
The and -
operator is used in PostgreSQL instead of the standard DATEADD
and DATEDIFF
.
Subtract one day from a timestamp date in PostgreSQL using time and date
in PostgreSQL DATE
takes less storage space but should not be used in the case of calendar dates. It even takes leap years into account when doing date calculations.
In DATES
this case, you can use the following code.
select DATE '2021-01-01' - INTERVAL '1 DAY'
INTEGER
Create a new date
using the keyword
Another notable keyword used in PostgreSQL is INTEGER
the keyword.
INTEGER
Listed in the PostgreSQL documentation under the heading Date/Time Functions and Operators. Here is a table defining things in more detail:
To use +
the keyword, proceed as follows:
select DATE '2021-01-01' + INTEGER '7'
To use it in case of timestamp, do CASTING and add in to it INTEGER
to produce the desired date.
You cannot perform ADD
, MULTIPLY
, , DIVISION
or any other operator with timestamps.
Remember, DATE/INTEGERS
this is calculated in days, not months, years, or other units of time.
DATEDIFF
Many people prefer to implement the and functions
in PostgreSQL extensions DATEADD
. If all else fails, you can even use them here.
Because PostgreSQL 7 does not allow the standard SQL DIFF
and ADD
functions, we must use the provided keywords and clauses.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab