JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

PostgreSQL query between date ranges

Author:JIYIK Last Updated:2025/04/10 Views:

In this article, we will discuss the different types of ranges in PostgreSQL.

Range types in PostgreSQL

By default, Postgres provides some ranges to compare these values.

Postgres has a daterangedate type to compare dates. Also, we can use betweento compare dates.

To demonstrate, let's create a table and populate it with some data.

Schema SQL:

CREATE TABLE logger(
    id SERIAL PRIMARY KEY,
    name VARCHAR (255) NOT NULL,
    login_date DATE NOT NULL DEFAULT CURRENT_DATE
);

Once the table is populated, execute this Postgres Query SQL.

select * from logger;

Output:

 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)

INTERVALDifferences between using and date types in Postgres

Let's say you want all the names and idwho logged into the database and how many days it took so far, and you want to see only the people who logged in in the last 120 days.

Query SQL:

SELECT id, name, now() - login_date as time_spent from logger
WHERE login_date> (CURRENT_DATE - INTERVAL '120 days');

You can put 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)

Used in Postgres date typesbetween

If you want to see who logged in between 2021 and the current date, you can run the following SQL command.

Query SQL:

WHERE login_date between '2021-01-01' AND CURRENT_DATE;

Output:

 id | name | login_date
----+------+------------
  3 | Bon  | 2021-06-06
  5 | Jene | 2022-02-22
  6 | Dan  | 2022-01-20
(3 rows)

The format of this date data type is YYYY-MM-DD. So when you try to insert or write queries, make sure you use the format supported by Postgres database.

Here is the documentation for the date data type format in Postgres.

Used in Postgres date typesdaterange

Let's say you want to see users who logged in between a date range. For example, June 6, 2021 to March 10, 2022.

Execute the following SQL command:

Query SQL:

SELECT *
FROM logger
WHERE '[2021-06-06, 2022-03-10]'::daterange @> login_date;

We used ::daterange, which means we converted the range type to date data type. @>It is called range operator and it can also be used for other range queries on different data types.

Output:

 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 CURRENT_DATE. You just need to remember that within the square brackets, the first date is the start date of the range and the second date is the end date of the range.

For the end date you can write that as well infinity.

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.

Article URL:

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial