SQL SERVER – Simple Method to Find FIRST and LAST Day of Current Date

I calculate the FIRST and LAST Day of Current Date using explicit date boundaries. The historical sample starts on October 28, 2017.

Gouache illustration of the first and last day of the current date's month, using the first and last sheets between two bookends.

DECLARE @Date datetime = '20171028';
SELECT @Date AS GivenDate,
       CONVERT(date, DATEADD(day, 1 - DAY(@Date), @Date)) AS FirstDayOfMonth,
       EOMONTH(@Date) AS LastDayOfMonth;
Original October 2017 month-boundary result.
Original October 2017 month-boundary result.

Interpret the first and last day for the current date

The expected boundaries are 2017-10-01 and 2017-10-31. DATEADD moves to day one, while converting to date removes time. EOMONTH returns the last day as a date. It requires SQL Server 2012 or later.

My old arithmetic retained time when the input was not midnight. It also did not generalize to date subtraction syntax. The explicit expressions avoid those assumptions. Substitute the current server date when required.

For a timestamp column, filter from the first day up to, but excluding, the first day of the next month. Comparing with midnight on the last day can omit that entire remaining day. The linked methods cover older releases.

Check the calendar and the reporting boundary

The example answers a monthly question. Its input is one point within October, and its outputs identify that month’s two calendar boundaries. These results do not identify the beginning and end of the input’s individual day. Make that distinction explicit when using the expressions in a report.

Month lengths also vary. For February 17, 2020, the corresponding boundaries are February 1 and February 29 because 2020 is a leap year. For February 17, 2021, they are February 1 and February 28. A fixed number of days cannot represent both cases correctly. The built-in month-end calculation follows the calendar.

For a December report, the exclusive upper boundary belongs to January of the following year. That is expected. It allows every timestamp within December to be considered while excluding the following month’s values. Keeping the upper comparison exclusive avoids inventing a final fractional second that depends on the column’s precision.

I test a non-midnight input as well as an ordinary calendar value before reusing older arithmetic. Check the returned types and the intended business period separately from the display format. If the application groups UTC events by a local reporting period, decide the time-zone boundary before applying the filter.

Reference: Microsoft’s EOMONTH documentation.

Related reading

A last-day date is not the end of that day, it is midnight when compared with a timestamp.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL DateTime, SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Remove Duplicate Rows Using UNION Operator
Next Post
SQL SERVER – Restoring SQL Server 2017 to SQL Server 2005 Using Generate Scripts

Related Posts

12 Comments. Leave new

  • Hi Pinal,

    Nice one, especially the logic used to calculate First day of the Current Date.

    Thanks,
    Srini

    Reply
  • Select
    Dateadd(dd, datediff(dd, 0, getdate()), 0)

    Will return today at midnight.
    Replace the two occurrences of dd with MM to get the beginning of the month.
    Need to round a time to the beginning of the minute? Replace the dd with minute.
    Beginning of the quarter? QQ.
    Beginning of the year? YY.
    If I recall correctly, logic works the same also with beginning of the week.

    Here’s how it works:
    datediff(dd, 0, getdate())
    Returns the number of date intervals (days, in this case) between date 0 (sql server’s epoch) and current date (or any other date you give it).

    To explain the next step, let’s say that returns 1701

    Dateadd(dd, 1701, 0)
    Will return the date that’s 1701 time intervals (days, here) since the epoch, date 0.

    Reply
  • This was very helpful. Thanks!

    Reply
  • Glad I can help!

    Reply
  • LARABA Mohammed Séddik (@mslaraba)
    April 25, 2020 11:40 pm

    a life saver, thank you :D

    Reply
  • Tks (Obrigada) :D

    Reply
  • First day of current month doesn’t work when you pass first day of the month or you want to use GETDATE() in the first day of a month

    Reply
  • Use that to get the first day SELECT dateadd(day,datediff(dd, 0, getdate()),-1)

    Reply
  • @Houman, this seems to work just fine:
    select dateadd(month,datediff(month, 0, ‘2021-02-01’), 0)

    Reply
  • @DATE-DAY(@DATE) does not work for a “date” datatype

    Reply
  • DECLARE @DATE DATETIME
    SET @DATE=’2022-04-25′
    SELECT @DATE AS GIVEN_DATE
    , DATEADD(d,1,EOMONTH(@DATE)) AS FIRST_DAY_OF_MONTH
    , EOMONTH(@DATE) AS LAST_DAY_OF_MONTH

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.