Languages
[Edit]
EN

PostgreSQL - extract decade from date/time value

0 points
Created by:
marc_so
234

In this article, we would like to show you how to extract decade from DATE, TIMESTAMP, TIME or INTERVAL in PostgreSQL.

Quick solution:

SELECT EXTRACT('decade' FROM "column_name")
FROM "table_name";
PostgreSQL - extract decade from timestamp/interval
Table used in the example - HeidiSQL

Note:

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

 

Practical example

In this example, we will only select decades from each record in the date column.

Query:

SELECT EXTRACT('decade' FROM "event_datetime") AS "decade"
FROM "events";

Output:

PostgreSQL - extract decade from timestamp/interval
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 
	('1450-01-01'),
	('1563-01-02'),
	('1569-01-03'),
	('1600-01-04'),
	('1601-01-05'),
	('1850-01-06'),
	('1950-01-07'),
	('2021-01-08');

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