Languages
[Edit]
EN

PostgreSQL - extract day from date/time value

0 points
Created by:
Tehya-Blanchard
444

In this article, we would like to show you how to extract day(1-31) from DATE, TIMESTAMPTIME or INTERVAL in PostgreSQL.

Quick solution:

SELECT EXTRACT('day' FROM "column_name")
FROM "table_name";
PostgreSQL - example data used to extract HOUR from DATETIME, TIMESTAMP or TIME
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 days from each record in the event_datetime column

Query:

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

Output:

PostgreSQL - extract day from DATE / TIMESTAMP / TIME
Results - HeidiSQL

Database preparation

create_tables.sql file:

CREATE TABLE "events"(
	"id" SERIAL PRIMARY KEY,
	"event_datetime" TIMESTAMP
);

insert_data.sql file:

INSERT INTO "events"
	("event_datetime") 
VALUES 
	('2021-01-01 11:41:31'),
	('2021-01-02 12:42:32'),
	('2021-01-03 13:43:33'),
	('2021-01-04 14:44:34'),
	('2021-01-05 15:45:35'),
	('2021-01-06 16:46:36'),
	('2021-01-07 17:47:37'),
	('2021-01-08 18:48:38'),
	('2021-01-09 19:49:39'),
	('2021-01-10 20:50:40');

Alternative titles

  1. PostgreSQL - extract day 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