Languages
[Edit]
EN

PostgreSQL - extract century from date/time value

0 points
Created by:
marc_so
864

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');

Alternative titles

  1. PostgreSQL - extract century from timestamp/interval
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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