Languages
[Edit]
EN

PostgreSQL - compare date values

0 points
Created by:
Haris65
416

In this article, we would like to show you how to compare date values in PostgreSQL.

Quick solution:

SELECT *
FROM "table_name"
WHERE "date_column_name" <= 'yyyy-mm-dd';

Note:

You can replace the <= operator with any other comparison operator.

Practical example

To show how to compare date values, we will use the following table:

PostgreSQL - example data used to compare date values

Note:

At the end of this article you can find database preparation SQL queries.

Example 1

In this example, we will display all the dates before 2000-01-04.

Query:

SELECT *  
FROM "dates" 
WHERE "date" < '2000-01-04';

Output:

PostgreSQL - compare datetime values - result
PostgreSQL - compare date values - result

Example 2

In this example, we will select all the rows from 2000-01-04 inclusive and display them without time using DATE() function (yyyy-mm-dd format only).

Query:

SELECT "date"
FROM "dates"
WHERE "date" <= '2000-01-04';

Output:

PostgreSQL - compare datetime values
results - HeidiSQL

Database preparation

create_tables.sql file:

create TABLE "dates"(
	"id" SERIAL PRIMARY KEY,
	"date" DATE
);

insert_data.sql file:

insert into "dates"
	("date") 
VALUES 
	('2000-01-01'),
	('2000-01-02'),
	('2000-01-03'),
	('2000-01-04'),
	('2000-01-05'),
	('2000-01-06');

PostgreSQL - dates

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join