Forum

How to Get the Firs...
 
Share:
Notifications
Clear all

How to Get the First and Last Day of the Month in Oracle SQL

1 Posts
1 Users
0 Reactions
1,278 Views
Posts: 134
Admin
Topic starter
(@sql-admin)
Reputable Member
Joined: 6 years ago

Retrieving the first and last day of a month is a frequent requirement in Oracle SQL, especially when dealing with monthly reports, billing cycles, or filtering date-based records. Fortunately, Oracle provides easy-to-use date functions that allow this with minimal code.


First Day of the Current Month

To get the first day of the current month, you can use the TRUNC function:

SELECT TRUNC(SYSDATE, 'MM') AS first_day
FROM dual;
  • SYSDATE returns the current system date and time.

  • TRUNC(SYSDATE, 'MM') removes the day and time components, returning the first day of the month.

  • Example output: 01-JUL-2025


Last Day of the Current Month

To get the last day of the month, Oracle provides the LAST_DAY function:

SELECT LAST_DAY(SYSDATE) AS last_day
FROM dual;
  • This function directly gives you the last calendar day of the current month.

  • Example output: 31-JUL-2025


Get First and Last Day of a Specific Month

If you're working with a specific date and want the first and last day of that month:

SELECT 
   TRUNC(TO_DATE('15-MAR-2024', 'DD-MON-YYYY'), 'MM') AS first_day,
   LAST_DAY(TO_DATE('15-MAR-2024', 'DD-MON-YYYY')) AS last_day
FROM dual;

This is useful in scheduled procedures or reports where the date reference is dynamic or provided by users.


Filter Records Within the Current Month

You can also use these functions to filter records that fall within the current month:

SELECT *
FROM orders
WHERE order_date BETWEEN TRUNC(SYSDATE, 'MM') AND LAST_DAY(SYSDATE);

This ensures that your report always reflects the current month's data without manual date updates.


Benefits of Using TRUNC and LAST_DAY in Oracle

  • Dynamically handles month start and end without hardcoding

  • Ideal for automated reports and monthly comparisons

  • Improves code readability and maintainability


Summary Table

Use CaseSQL Function Used
First day of monthTRUNC(SYSDATE, 'MM')
Last day of monthLAST_DAY(SYSDATE)
Custom date rangeUse TRUNC + LAST_DAY on input date

 


Leave a reply

Author Name

Author Email

Title *

 
Preview 0 Revisions Saved
Share: