How to Get the First Day of the Month in PL/SQL?
Last Updated :
22 Oct, 2024
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 achieve this task using PL/SQL, providing readers with comprehensive methods to handle date manipulation effectively.
How to Get the First Day of the Month in PL/SQL
When working with dates in PL/SQL, extracting the first day of the month from a given date can be essential for many applications. To address this need, we'll discuss three approaches:
Approach 1: Using TRUNC() Function
The TRUNC() function is the simplest and most commonly used approach for obtaining the first day of the month in PL/SQL. The TRUNC() function truncates a date to the specified unit (e.g., day, month, year), allowing you to extract only the desired portion of the date.
Syntax:
TRUNC(date_expression, 'MONTH')
- date_expression: The date from which the first day of the month is to be extracted.
- 'MONTH': The unit to truncate the date to (in this case, the month).
Query:
In this example, the current system date (SYSDATE) is truncated to the first day of the month using the TRUNC function. The resulting data is then displayed using the DBMS_OUTPUT.PUT_LINE statement.
DECLARE
first_day DATE;
BEGIN
first_day := TRUNC(SYSDATE, 'MONTH');
DBMS_OUTPUT.PUT_LINE('First day of the month: ' || TO_CHAR(first_day, 'DD-MON-YYYY'));
END;
Output:
PL/SQL using TRUNC()Explanation:
- The TRUNC() function is used to truncate the given date to the specified unit, in this case, 'MONTH', which makes sure that only the first day of the month remains.
- In the example, we use the SYSDATE function to get the current system date. We then apply TRUNC(SYSDATE, 'MONTH') to obtain the first day of the current month.
- The resulting date is assigned to the variable first_day, which is then displayed using the DBMS_OUTPUT.PUT_LINE() function.
- The TO_CHAR() function is used to format the date in the desired output format.
The EXTRACT() function allows you to extract specific components (e.g., year, month) from a date. You can use this to construct the first day of the month by extracting the year and month and combining them with a fixed day value.
Syntax:
TO_DATE(EXTRACT(YEAR FROM date_expression) || '-' || EXTRACT(MONTH FROM date_expression) || '-01', 'YYYY-MM-DD')
- EXTRACT(YEAR FROM date_expression): Retrieves the year component from the date.
- EXTRACT(MONTH FROM date_expression): Retrieves the month component from the date.
- TO_DATE(): Converts the concatenated string to a DATE data type.
Query:
DECLARE
month_num NUMBER;
year_num NUMBER;
first_day DATE;
BEGIN
SELECT EXTRACT(MONTH FROM SYSDATE) INTO month_num FROM DUAL;
SELECT EXTRACT(YEAR FROM SYSDATE) INTO year_num FROM DUAL;
first_day := TO_DATE('01-' || month_num || '-' || year_num, 'DD-MM-YYYY');
DBMS_OUTPUT.PUT_LINE('First day of the month: ' || TO_CHAR(first_day, 'DD-MON-YYYY'));
END;
Output:
PL/SQL using EXTRACT()Explanation:
- Here, the EXTRACT() function is used to extract the month and year components from the current system date.
- We then construct a new date string with the first day of the month using these extracted values.
- This constructed date string is converted back to a DATE data type using the TO_DATE() function.
- Finally, the first_day variable is displayed using DBMS_OUTPUT.PUT_LINE() after formatting it using TO_CHAR() function.
Approach 3: Using To_Char()
The TO_CHAR() function can be used to format a date and extract specific components (such as month and year). By concatenating these components with a fixed day value, you can construct the first day of the month.
Syntax:
TO_DATE('01-' || TO_CHAR(SYSDATE, 'MM-YYYY'), 'DD-MM-YYYY')
- TO_CHAR(date_expression, 'MM-YYYY'): Formats the date to return the month and year.
- TO_DATE(): Converts the constructed string back to a DATE type
Query:
In this example, we will use To_Char() functions to format the Date retrieved from To_Date to To_Char for converting it from date type to Char type.
DECLARE
first_day DATE;
BEGIN
first_day := TO_DATE('01-' || TO_CHAR(SYSDATE, 'MM-YYYY'), 'DD-MM-YYYY');
DBMS_OUTPUT.PUT_LINE('First day of the month: ' || TO_CHAR(first_day, 'DD-MON-YYYY'));
END;
Output:
PL/SQL using TO_CHAR()Explanation:
- In this approach, the TO_CHAR() function is utilized to format the current system date and extract the month and year components.
- We then concatenate '01-' with the month and year obtained to form a string representing the first day of the month.
- This string is then converted back to a DATE data type using the TO_DATE() function.
- Finally, the first_day variable is displayed using DBMS_OUTPUT.PUT_LINE() after formatting it using TO_CHAR() function.
Conclusion
Knowing how to retrieve the first day of the month in Oracle is essential for date-related operations like report generation, billing, and scheduling. This article covered three approaches to achieve this:
- Using the TRUNC() function: The simplest and most efficient way to get the first day of the month.
- Using the EXTRACT() function: Useful for building dates dynamically.
- Using the TO_CHAR() function: Allows for custom date formatting.
Similar Reads
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 th
4 min read
How to Get First Day of Every Corresponding Month in MySQL? In the MySQL database, we generally neÂed to get the first day of a month from a ceÂrtain date. While there are many tasks related to dates, one key job is figuring out the first day of each month is an important need. This is important in many placeÂs like finance reports or scheÂdules. This is us
4 min read
SQL Query to Get First and Last Day of a Month in a Database In SQL, working with date and time data is critical for tasks like reporting, scheduling, and data analysis. Determining the first and last day of a month is a common requirement. SQL provides powerful functions like DATE_SUB, DAYOFMONTH, and LAST_DAY to make these calculations straightforward.In th
4 min read
How to Get Number of Days in Current Month in PHP? This article will show you how to get the number of days in the current month in PHP. In PHP, you may need to find the number of days in the current month for various purposes, such as generating a calendar or performing date calculations. There are three approaches to get the number of days in the
3 min read
Program to find the number of days in a given month Write a program to find the number of days in a given month. We are given name of a month in lowercase letters, so print the number of days in the month. Note: For February, consider the number of days as 28. Examples: Input: month = "january"Output: 31Explanation: January has 31 days. Input: month
3 min read