PostgreSQL

Postgres between date range

This article will guide using the between command with the date range in PostgreSQL. In this article, we will be implementing this command with several types of date ranges to understand the logic behind the concept. The “BETWEEN” command is used to select values inside the range specified after the ” BETWEEN ” keyword is entered. This command can work with all sorts of values like numbers, dates, or text. The “ BETWEEN “ command simplifies the data values as the output is filtered with the value range check, and we have more clarity in this type of operation.

Using BETWEEN command with the date as the range in PostgreSQL:

The “ BETWEEN ” command is usually used as a subquery and is used by other commands such as “ SELECT ”, “ WHERE “ and “ FROM ”. The keyword “ AND ” is always used as a separator between the range that we provide from high to low values in the PostgreSQL syntax.

Since we have to implement this command in the PostgreSQL environment, we have to have a table and some values in it which we can use for the “ BETWEEN ” command. We will use several date types and arrangements to portray the use of this command in between those ranges. Take a look at the example provided below:

Example 1:

In this example, we will use the standard date format in PostgreSQL, “YYYY-MM-DD ”. We will create a table with 2 columns for an ID and a date to implement this example. Then we will insert several values in the table using the code below:

CREATE TABLE empdate (

  e_id int,

  emp_date date

);

INSERT INTO empdate values (1,'2018-05-08'),

(2,'2019-05-08'),

(3,'2015-05-04'),

(4,'2020-07-08'),

(5,'2021-04-07');

Text Description automatically generated

Attached is the output of the above query.

A screenshot of a computer screen Description automatically generated with medium confidence

After successfully inserting entries into the table, we will utilize the ” BETWEEN ” command in

conjunction with other subqueries like as the ” SELECT “, ” FROM “, ” WHERE “, and ” AND ”

instructions to create a query. We will use the above table as an example and run the following

query:

SELECT

e_id,

emp_date

FROM

empdate

WHERE

emp_date BETWEEN '2015-01-01' AND '2019-01-01';

A picture containing text Description automatically generated

In this query, we will be giving a range to the “ SELECT ” command from which we can filter out the output and narrow it down. We will retrieve the ” e_id ” and ” emp_date ” from the table we made before, but only the dates between ‘2015-01-01’ and ‘2019-01-01’ will be present in the output.

A screenshot of a computer screen Description automatically generated with medium confidence

As the output suggests that only two “ e_id ” are present in the table with dates between the given range. This query helped us filter out the given values and give a more processed view of the table with which we can easily operate.

With ” DELETE “, ” FROM “, and ” WHERE “, we will apply the ” BETWEEN ” command as a subquery. The “ DELETE ” command will use the date range given by the “ BETWEEN ” command and delete the values present in between that range. For this method, we will use the query given below:

Delete from empdate

WHERE

emp_date BETWEEN '2015-01-01' AND '2018-01-01';

A picture containing logo Description automatically generated

This query will delete the rows from the “ empdate ” table whose dates are between ‘2015-01-01’ and ‘2018-01-01’

A screenshot of a computer screen Description automatically generated with medium confidence

As you can see in the output above, we have successfully removed a row from a table that was present in between the date range provided in the query.

Example 2:

Now we will use the date in the standard format with the time as well, but we will not be selecting the time zone. We will create a table with two columns, one for the id and the second for the date, and will also insert some rows in the table to alter them with our further queries.

CREATE TABLE Customer_acc (

  c_id int,

  acc_date TIMESTAMP

);

INSERT INTO Customer_acc values (102,'2018-05-08 05:00:00'),

(103,'2019-05-08 06:00:00'),

(101,'2017-03-02 12:50:00');

Select * from Customer_acc

Text Description automatically generated

Attached is the output of the above query.

A screenshot of a computer screen Description automatically generated with medium confidence

After creating a table and inserting values in it, we will use the “ SELECT “ and “ BETWEEN ” commands to filter some rows in the table that we created. For this method, we will use the query given below:

SELECT

c_id,

acc_date

FROM

Customer_acc

WHERE

acc_date BETWEEN '2015-03-01' AND '2019-02-15';

A picture containing text Description automatically generated

The rows between ‘2015-03-01’ and ‘2019-02-15’ will be filtered in this query.

A screenshot of a video game Description automatically generated with medium confidence

In this output, we can see the filtered view of the table because of the “ BETWEEN ” command. Now we will use the “ Delete ” and “ BETWEEN ” command together on the “ Customer_acc ” table to see the effect of these commands on the modified date range.

Delete from Customer_acc

WHERE

acc_date BETWEEN '2015-03-01' AND '2018-05-08';

Text Description automatically generated

With the help of this query, we will be deleting the rows between the ‘2015-03-01’ and ‘2018-05-08’ date range. The below output suggests that we have successfully deleted the value that was present between the range given in the query.

Graphical user interface Description automatically generated with medium confidence

Example 3:

We will now utilize the data in standard format with the time and the time zone. We will make a table with two columns, one for the id and the other for the date, and then we will add some rows to it so we can change it with our other queries.

CREATE TABLE t_data (t_id int, t_date TIMESTAMPTZ);

SET timezone = 'America/Chicago';

INSERT INTO t_data values (102,'2018-05-08 02:30:00'),

(103,'2019-05-08 21:00:00'),

(101,'2017-03-02 19:50:00');

SELECT * FROM t_data;

Text Description automatically generated

Attached is the output of the above query.

A screenshot of a computer screen Description automatically generated with medium confidence

Now to filter particular rows in the table we created, we will use the ” SELECT ” and ” BETWEEN” commands. After we have created the table and added rows to it. We will use the following query for this method:

SELECT

t_id,

t_date

FROM

t_data

WHERE

t_date BETWEEN '2015-01-01 01:00:00' AND '2019-02-15 10:00:00';

Text Description automatically generated

Attached is the output of the above query.

A screenshot of a computer screen Description automatically generated with medium confidence

Because of the ” BETWEEN ” command, we can see the filtered view of the table in this output. On the ” t_data ” table, we will use the ” Delete ” and ” BETWEEN ” commands together to observe how they affect the changed date range.

Delete from t_data

WHERE

t_date BETWEEN '2015-01-01 01:00:00' AND '2019-02-15 10:00:00';

Select * from t_data;

Text Description automatically generated

We will delete the rows between the dates ‘2015-01-01 01:00:00’ and ‘2019-02-15 10:00:00’ using this query. As shown in the result below, we were successful in deleting the value that was present between the ranges specified in the query.

Graphical user interface Description automatically generated

Conclusion:

This article provided a guide on using the “ BETWEEN ” with several other queries in PostgreSQL. We implemented this command with different types of dates. First, we used a standard date format in PostgreSQL with the “ BETWEEN ” command. Then, we modified the data range by using timestamps with and without setting the timezone to better understand this command. We have concluded that the “ BETWEEN ” command can be used with almost every variation of the date range and provide us with a simplified and filtered view of the table.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.