How to Extract Day of Week From Date Field in PostgreSQL?
Last Updated :
20 Feb, 2024
In PostgreSQL, extracting the day of the week from a date field is a common and essential task when working with temporal data. Knowing the day of the week is crucial for various applications, including scheduling, reporting, and analytical purposes. PostgreSQL provides multiple methods to achieve this, offering flexibility based on specific requirements.
This article explores three common techniques: using the EXTRACT function, the TO_CHAR function, and the DATE_PART function, each catering to different scenarios for extracting day-of-week information from date fields in PostgreSQL.
How to Extract the Day of the Week from Date Fields Using PostgreSQL?
In PostgreSQL, you can extract the day of the week from a date field using various methods. Here are some common ways to achieve this :
- Using the EXTRACT Function
- Using the TO_CHAR Function
- Using the DATE_PART Function
To understand How to Extract day of week from date field in PostgreSQL we need a table on which we will perform various operations. So we create a table example_table.
Creating the table example_table
CREATE TABLE example_table (
sample_dates date
);
Inserting values in example_table
INSERT INTO example_table (sample_dates)
VALUES ('2024-02-07'), ('2024-02-08'), ('2024-02-09');
Using the Extract() Function
In PostgreSQL, using the EXTRACT function and the 'DOW' (day of the week) specifier, you may extract the day of the week from a date field. The outcome will be a number that indicates the day of the week, with Saturday being 6 and Sunday being 0.
Syntax:
SELECT EXTRACT(DOW FROM column_name) FROM table_name;
Here replace table_name with the name of the table and column_name with name of the column that has date data type.
Example: Extracting Day of Week from Date Field in PostgreSQL
SELECT sample_dates,EXTRACT(DOW FROM sample_dates) AS day_of_week
FROM example_table;
Output:
Output for Extract functionExplanation: The table below shows the representation of output numbers concerning corresponding days in a week.
Using the TO_CHAR() Function
In PostgreSQL, you can format a date or timestamp as a string by using the TO_CHAR function. It can be utilized to extract the day of the week in a textual representation.
Syntax:
SELECT TO_CHAR(column_name, Format Specifier)
FROM table_name;
Here replace table_name with the name of the table and column_name with name of the column that has date data type.
Some Format Specifiers can be the following :
1. 'Day': Full name of the day of the week (e.g. "Tuesday").
2. 'day': Full name of the day of the week in lowercase(e.g. "Tuesday").
3. 'DAY': Full name of the day of the week in UPPERCASE(e.g. "TUESDAY").
4. 'Dy': Three-letter abbreviation of the day of the week (e.g. "Tue").
5. 'dy': Three-letter abbreviation of the day of the week in lowercase (e.g. "true").
6. 'DY': Three-letter abbreviation of the day of the week in UPPERCASE(e.g. "TUE").
Example: Displaying Day of Week as Text from Date Field in PostgreSQL
SELECT sample_dates,TO_CHAR(sample_dates, 'Day') AS day_of_week
FROM example_table;
Output:
Output of TO_CHAR FunctionExplanation: The provided SQL query creates a new column called day_of_week and pulls the sample_dates column from the example_table. The date data in sample_dates are formatted into their corresponding complete day names using the TO_CHAR function. The output shows a list of dates combined with the matching day of the week, presenting a legible picture of the weekdays that correspond to each date in the given data.
Using the Date_Part() Function
In PostgreSQL, A useful tool for removing particular elements from a date or timestamp is the DATE_PART function. Particularly helpful are the 'dow' and 'isodow' parameters for getting the day of the week.
1) Day Of Week (Sunday to Saturday, 0 to 6):
Syntax:
SELECT DATE_PART('dow', column_name) AS day_of_week
FROM your_table;
Replace your_table with the name of the actual table and column_name with the name of the actual column that contains the date field. The outcome will be an integer with Sunday being 0 and Saturday being 6, signifying the day of the week.
Example: Extracting Day of Week using DATE_PART in PostgreSQL
SELECT sample_dates,DATE_PART('dow', sample_dates) AS day_of_week
FROM example_table;
Output:
Output of DATE_PART Function with dowExplanation: The given SQL statement creates a new column called day_of_week and pulls the sample_dates column from the example_table. For each date in the given table, the DATE_PART function is used to extract the day of the week as an integer (0 for Sunday, 1 for Monday, and so on). The outcome shows the original dates next to the day of the week represented by a number.
2) ISO Day Of Week (Monday to Sunday, 1 to 7):
Syntax:
SELECT DATE_PART('isodow', column_name) AS day_of_week
FROM your_table;
Replace your_table with the name of the actual table and column_name with the name of the actual column that contains the date field. The outcome will be an integer with Monday being 1 and Sunday being 7, signifying the day of the week.
Example: Retrieving the ISO Day of the Week Using DATE_PART Function in PostgreSQL
SELECT sample_dates,DATE_PART('isodow', sample_dates) AS day_of_week
FROM example_table;
Output:
Output of DATE_PART Function with isodowExplanation: This SQL statement creates a new column called day_of_week by using the DATE_PART function with the 'isodow' argument. It also picks the sample_dates column from the example_table. It provides a numerical depiction of weekdays by extracting the ISO day of the week (which ranges from 1 for Monday to 7 for Sunday) for every date in the table.
Conclusion
In Conclusion, for procedures requiring numerical comparisons, the EXTRACT function is useful in extracting the numerical representation of the day of the week. In contrast, the TO_CHAR function offers options for retrieving the whole day name, three-letter abbreviations allowing for output customisation. The DATE_PART function in PostgreSQL facilitates efficient extraction of the day of the week, offering both numeric and ISO representations for diverse applications.
Similar Reads
How to Find Day Name From Date in SQL Server?
Finding the day name from a specific date is a common task in SQL Server, useful for generating reports, analyzing trends, or scheduling. SQL Server provides two primary methods such as the DATENAME() function and the FORMAT() function. In this article, We will learn about How to Find Day Name From
4 min read
How to Format a Date in PostgreSQL
PostgreSQL provides powerful tools to format dates into different styles and patterns using functions like TO_CHAR(). Formatting dates in PostgreSQL is important when we need to present data in a more readable or application-friendly way. This article will explain how to format a date in PostgreSQL
4 min read
How to Find Day Name from Date in Python
In Python programming, we may frequently need to know which day of the week corresponds to a particular date. This can be useful for a variety of applications, including task scheduling, trend analysis, and showing dates in an easy-to-read style. Get Day Name from Date in PythonTo get the day name f
3 min read
How to Exclude Weekend Days in a SQL Server Query?
With this article, we will learn how to exclude weekend days in a SQL server query. For this task, we use the DATEADD ( ) MS.SQL server function. This function in SQL Server is used, to sum up, a time or a date interval to a specified date then returns the modified date. Syntax : DATEADD(interval, n
2 min read
How to Group by Day, Date, Hour, Month or Year in SQL Server
Grouping data by day, date, hour, month, or year in SQL Server involves using the GROUP BY clause and appropriate date functions to extract or manipulate the relevant portions of the datetime column. The SQL Server GROUP BY operator is similar to the SQL GROUP BY Operator. The GROUP BY clause is a p
3 min read
Excel Formula to Get Next Day of Week
MS-Excel is a program that is part of the Microsoft Office suite. It is a multi-row and multi-column electronic spreadsheet that is used to organize data, graphically portray data(s), and conduct various calculations. It has 1048576 rows and 16383 columns, with each row and column forming a cell. Ea
3 min read
How to Get Week Numbers from Dates in R?
Week number is the number of weeks within a year or in a set period. It indicates that people of different countries and organizations may have other ways to account for the weeks however, the most popular one is the ISO 8601. In this standard. When it comes to the extraction of week numbers in R, t
3 min read
How to get the Day of the Week in jQuery ?
The Date() constructor will not give the day of the week directly. It returns a number for the weekdays starting from 0 to 6. You need to implement some extra methods in jQuery to get the day of the week as listed below: Table of Content Using Date.getDay() methodUsing moment.jsUsing Date.getDay() m
2 min read
How to Get the Day of the Week for PL/SQL?
PL/SQL is a Procedural language extension for the Structured Query Language (SQL) that allows for procedural programming in Oracle databases. This language can create tables and manipulate the data stored in the tables using queries or statements like insert, delete, update, alter, etc. The day of t
4 min read
Convert Date to Day of Week in R
In this article, we are going to see different approaches to convert date to day of the week in R Programming language. The three different types of approaches to convert given dates to the day of the week in R language are as follows: Converting date to day of the week using weekdays functionConver
3 min read