Querying between date ranges in PostgreSQL
This article will discuss different types of ranges to compare dates in PostgreSQL.
Date ranges in PostgreSQL
By default, PostgreSQL provides some ranges to compare values. In particular, we can use daterange
and between
to compare dates.
To demonstrate, let's create a table and populate it with some data.
CREATE TABLE logger(
id SERIAL PRIMARY KEY,
name VARCHAR (255) NOT NULL,
login_date DATE NOT NULL DEFAULT CURRENT_DATE
);
Output:
postgres=# select * from logger;
id | name | login_date
----+-------+------------
1 | Jhon | 2020-06-06
2 | Alice | 2022-06-06
3 | Bon | 2021-06-06
4 | Trude | 2020-02-02
5 | Jene | 2022-02-22
6 | Dan | 2022-01-20
(6 rows)
Query date range using date intervals and differences in PostgreSQL
Let's say you want all the names (with id) of people who have logged into the database and the number of days they have logged in till today. You only want to see users who have logged in in the last 120 days.
Sample code:
SELECT id, name, now() - login_date as time_spent from logger
WHERE login_date> (CURRENT_DATE - INTERVAL '120 days');
Here you can enter hours, days, months, and years in the interval.
Output:
id | name | time_spent
----+------+-------------------------
5 | Jene | 21 days 11:44:35.790685
6 | Dan | 54 days 11:44:35.790685
(2 rows)
between
Querying date ranges
in PostgreSQL using
You can run the following SQL command to see who logged in between 2021 and the current date.
postgres-# WHERE login_date between '2021-01-01' AND CURRENT_DATE;
id | name | login_date
----+------+------------
3 | Bon | 2021-06-06
5 | Jene | 2022-02-22
6 | Dan | 2022-01-20
(3 rows)
Here the format of date data type is YYYY-MM-DD. So when you try to insert or write query, make sure you use the format supported by PostgreSQL database.
Here is the documentation for the date data type format in PostgreSQL.
daterange
Querying date ranges
using the type in PostgreSQL
Now, suppose you want to see users who logged in between a date range, such as 2021-06-06 to 2022-03-10. Let's create the query.
SELECT *
FROM logger
WHERE '[2021-06-06, 2022-03-10]'::daterange @> login_date;
Here, we use ::daterange
, which means we convert the range type to date data type. @>
It is called range operator and can be used for range queries on other different data types as well.
Output:
postgres-# WHERE '[2021-06-06, 2022-03-10]'::daterange @> login_date;
id | name | login_date
----+------+------------
3 | Bon | 2021-06-06
5 | Jene | 2022-02-22
6 | Dan | 2022-01-20
(3 rows)
Additionally, you can use it in ranges as well CURRENT_DATE
. Remember, inside the square brackets, the first one is the start date and the second one is the end date of the range.
For the end date, you can also write infinity
. You can visit the website here to learn more about date ranges.
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
Killing a process ID in PostgreSQL
Publish Date:2025/04/27 Views:191 Category:PostgreSQL
-
Today, we will learn how to kill or stop a running query in the background when working with PostgreSQL database. This may happen if the frontend stops working but a background process is still running. In this case, you may want to kill th
How to install and deploy PostgreSQL as a Docker container
Publish Date:2025/04/27 Views:50 Category:PostgreSQL
-
PostgreSQL , also known as Postgres, is a leading object-relational database system. It is popular because it is highly compliant with the SQL standard and includes additional features that simplify processing complex data sets at scale. Po
Selecting whether a string contains a substring match in PostgreSQL
Publish Date:2025/04/27 Views:186 Category:PostgreSQL
-
Today, we will learn how to find a value in a PostgreSQL string and select it if it matches certain conditions. Suppose you have a string carabc and want to see if it contains a value car . Therefore, you will try to use a function that tel
Add unique constraint after creating table in PostgreSQL
Publish Date:2025/04/27 Views:63 Category:PostgreSQL
-
Today we will learn how to add constraints after the rows in a table have been created UNIQUE . The UNIQUE constraint guarantees that the data in a row is unique in that column. So if the column ID exists, all rows will have unique values,
Creating a Schema in PostgreSQL
Publish Date:2025/04/27 Views:196 Category:PostgreSQL
-
This article will discuss creating schemas in PostgreSQL using SQL queries or psql. CREATE SCHEMA Use the statement to create a pattern in SQL query To create a new schema, execute the following command. CREATE SCHEMA test_schema To view al
Changing User Password in Postgres
Publish Date:2025/04/27 Views:108 Category:PostgreSQL
-
In this article, we will change the user password in Postgres. Changing User Passwords in Postgres Using Windows Open from the menu or search bar SQL Shell (psql) . Connect to the default database using the default port. If you set it up wi
Changing column types in Postgres
Publish Date:2025/04/27 Views:91 Category:PostgreSQL
-
This article shows how to change a column type to another data type in Postgres. ALTER TABLE To change the column type in Postgres, use the command ALTER TABLE table_name ALTER COLUMN column_name [ SET DATA ] TYPE new_type ; Use 表名 , , 列
Importing SQL files in PostgreSQL
Publish Date:2025/04/27 Views:129 Category:PostgreSQL
-
This article discusses how to import SQL files in PostgreSQL. psql Import SQL files in PostgreSQL using command To import the SQL file, run the following command: psql - U dbuser - h localhost databasename filename. sql If the file is locat
Create a table if it does not exist in PostgreSQL
Publish Date:2025/04/27 Views:197 Category:PostgreSQL
-
PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitor MySQL. Apart from the above differences, when writing queries for PostgreSQL and MySQL or other database systems, the