EN
PostgreSQL - extract decade from date/time value
0
points
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";
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:
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');