There are several methods to retrieve the initial and final date of the preceding month in Oracle:
Using the SYSDATE function, subtracting the number of days in the current month and adding one day to get the initial date of the preceding month. Then, subtracting one day from the initial date of the current month to get the final date of the preceding month.
SELECT TRUNC(SYSDATE, 'MM') - 1 AS LastDayOfPrecedingMonth, TRUNC(SYSDATE, 'MM') - EXTRACT(DAY FROM SYSDATE) + 1 AS FirstDayOfPrecedingMonth FROM DUAL;
Using the ADDMONTHS function, subtracting one month and using the LASTDAY function to get the final date of the preceding month. Then, subtracting the number of days in the preceding month minus one from the final date to get the initial date of the preceding month.
SELECT LASTDAY(ADDMONTHS(SYSDATE, -1)) AS LastDayOfPrecedingMonth, LASTDAY(ADDMONTHS(SYSDATE, -1)) - (TONUMBER(TOCHAR(LASTDAY(ADDMONTHS(SYSDATE, -1)), 'DD')) - 1) AS FirstDayOfPrecedingMonth FROM DUAL;
Both methods will return the same results.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-02 11:00:00 +0000
Seen: 8 times
Last updated: Sep 12 '21
How can folders be created for each month using react pdf?
What is the process of scheduling a cron job to occur on the first Thursday of each month?
Can you provide the DateTimeImmutable object on a monthly basis?
How can a Hive script be written to obtain the month in the form of MMM?
Determine whether the given dates encompass a week, quarter, day, and month.
What is the method for obtaining the millisecond information from a date in Oracle?
How can Oracle Tables in bulk be transferred to Azure Storage using Azure Data Factory?