EN
PostgreSQL - extract day of week from date/time value
0 points
In this article, we would like to show you how to extract day of week
from DATE
, TIMESTAMP
or TIME
in PostgreSQL. Returns a result from 0 (Sunday)
to 6 (Saturday)
.
Quick solution:
xxxxxxxxxx
1
SELECT EXTRACT('dow' FROM "column_name")
2
FROM "table_name";

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will only select days of week
from each record in the event_datetime
column.
Query:
xxxxxxxxxx
1
SELECT EXTRACT('dow' FROM "event_timestamp") AS "day of week"
2
FROM "events";
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE "events"(
2
"id" SERIAL PRIMARY KEY,
3
"event_timestamp" TIMESTAMP
4
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO "events"
2
("event_timestamp")
3
VALUES
4
('2021-01-01 11:41:31'),
5
('2021-01-02 12:42:32'),
6
('2021-01-03 13:43:33'),
7
('2021-01-04 14:44:34'),
8
('2021-01-05 15:45:35'),
9
('2021-01-06 16:46:36'),
10
('2021-01-07 17:47:37'),
11
('2021-01-08 18:48:38'),
12
('2021-01-09 19:49:39'),
13
('2021-01-10 20:50:40');