To extract the day of the week from a date in PostgreSQL, you can use the DATE_PART() function with the ‘dow’ (day of the week) unit.
Syntax for DATE_PART() Function in PostgreSQL:
DATE_PART(unit, source)
unit: Specifies the part of the date or timestamp to extract (e.g., ‘year’, ‘month’, ‘day’, ‘hour’, ‘minute’, ‘second’, etc.).
source: The date or timestamp from which to extract the specified part.
In Our case. The values of the unit must “dow” (day of the week). The output will be in numbers where 0 Represents Sunday, 1 Represents Monday and so on. Refer below table for more details
Day of Week | Day Notation |
0 | Sunday |
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
Extract Day of week from Date in PostgreSQL Simple Example:
In order to Extract day of week from date in postgresql we will be using DATE_PART() function.
In the below Example we will be passing dow as “unit” argument and date as “source” argument in order to extract day of week from Date or datetime
SELECT DATE_PART('dow', '2024-02-24'::timestamp) AS day_of_week_part;
Output:
Extract Day of week from Current date in PostgreSQL :
This query uses the CURRENT_DATE function to get the current date and then extracts the day of the week using DATE_PART()
SELECT CURRENT_DATE AS current_date, DATE_PART('dow', CURRENT_DATE) AS day_of_week;
Output:
Extract Day of week from Date in PostgreSQL table:
We will be using Below Student_detail table for our example to depict on how to extract day of week from datetime in PostgreSQL
Student_detail:
In the above table we will be using DATE_PART() Function, which will take “dow” (day of week) and column named “birthdaytime” as argument. Which will extract day of the week from “birthdaytime” column and store in the new column of postgresql table as shown below
select *, DATE_PART('dow', birthdaytime::timestamp) AS day_of_week from Student_detail
so the resultant table will have day_of_week column