Languages
[Edit]
EN

PostgreSQL - extract century from date/time value

0 points
Created by:
Dirask Admin - Marvin
1940

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

Quick solution:

SELECT EXTRACT('century' FROM "column_name")
FROM "table_name";
PostgreSQL - extract century 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 centuries from each record in the date column.

Query:

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

Output:

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