How to Get the Day of the Week for PL/SQL?
Last Updated :
23 Jul, 2025
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 the week in PL/SQL can be used for a variety of database operations. In this article, we'll look at how to retrieve this data in PL/SQL. We will start with the basics, like the TO_CHAR function and EXTRACT function.
How to Retrieve the Day of the Week Using PL/SQL
Retrieving the day of the week from a given date is frequently encountered in database programming. In PL/SQL, there are multiple approaches to tackle this problem effectively.
1. Using the TO_CHAR function
The TO_CHAR function is a versatile tool in Oracle PL/SQL used for converting values to varchar datatype with various formatting options. It is particularly useful for formatting date values into readable strings.
Syntax
TO_CHAR(date_value, format_mask);
Example 1: Retrieving Day of the Week Using TO_CHAR Function
DECLARE
today_date DATE:= SYSDATE;
day_of_week VARCHAR2(10);
BEGIN
-- DY returns the first 3 characters
-- DAY format returns the full name of day of week
day_of_week := TO_CHAR(today_date, 'DY');
DBMS_OUTPUT.PUT_LINE('Day of the week: ' || day_of_week);
END;
Here, in the above code, two variables are declared where one variable is the date and another variable is for storing the day of week value after processing.
TO_CHAR functions take two arguments where one argument is the above context is the date and the format that needs to be changed is the second argument.
Output:
TO_CHAR function DY formatExplanation:
- The TO_CHAR function is utilized to convert the current date into a string representing the abbreviated day of the week ('DY' format).
- The result is then displayed using the DBMS_OUTPUT.PUT_LINE function.
Example 2: Retrieving Day of the Week Using TO_CHAR Function
DECLARE
today_date DATE:= SYSDATE;
day_of_week VARCHAR2(10);
BEGIN
-- DY returns the first 3 characters
-- DAY format returns the full name of day of week
day_of_week := TO_CHAR(today_date, 'DY');
DBMS_OUTPUT.PUT_LINE('Day of the week: ' || day_of_week);
END;
Output:
TO_CHAR function DAY formatExplanation: In the above outputs, we can observe that the DBMS_OUTPUT.PUT_LINE statement is used for returning the result and the 'DY' format returns the first 3 characters of the day of week string and the 'DAY' format returns the full name of the day of week.
The EXTRACT function is another powerful method in Oracle PL/SQL that allows you to retrieve specific components from a date, such as the day, month, or year. When extracting the day of the week, it returns an integer representing the day (1 for Sunday, 2 for Monday, etc.).
Syntax
EXTRACT(WEEKDAY FROM date_value)
DECLARE
my_date DATE:= SYSDATE;
day_of_week NUMBER;
BEGIN
day_of_week := EXTRACT(DAY FROM my_date);
DBMS_OUTPUT.PUT_LINE('Day of the week(numeric): ' || day_of_week);
DBMS_OUTPUT.PUT_LINE('Day of the week(full name): ' || TO_CHAR(my_date, 'DY'));
END;
Output:
Extract FunctionExplanation:
- The code retrieves the day of the week from the current date using the EXTRACT function and assigns it to the variable "day_of_week."
- It then prints the numeric representation of the day and its full name using the DBMS_OUTPUT.PUT_LINE function. This provides both numeric and textual representations of the day of the week.
Conclusion
PL/SQL, as a procedural language extension for SQL, offers powerful tools and functions for database manipulation tasks. By mastering techniques such as the TO_CHAR and EXTRACT functions, developers can efficiently retrieve the day of the week from dates within their Oracle PL/SQL programs. This not only enhances the functionality of database applications but also contributes to better data management and analysis.
Similar Reads
How to Get the First Day of the Month in PL/SQL? In PL/SQL programming, efficiently manipulating dates is crucial for various tasks, such as generating reports, calculating durations, or scheduling events. One common requirement is retrieving the first day of the month from a given date. In this article, we'll explore different approaches to achie
4 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 Day of Week Number in Moment.js? Moment.js is a popular and open-source JavaScript library for validating, manipulating, and formatting dates. One common task is to determine the day of the week for a given date. This can be particularly useful in applications that need to perform actions based on the day of the week. In this artic
2 min read
How To Get the Current Date Without Time in T-SQL? To get the current date in T-SQL use the GETDATE() function. The result of the function is DateTime data type meaning, it contains both the date and the time, e.g. 2022-07-10 10:32:04. However, to get the current date without time in T-SQL use the CAST() function. This function takes any expression
1 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 Extract Day of Week From Date Field in PostgreSQL? 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 t
5 min read