SQL SERVER – Find Last Day of Any Month – Current Previous Next

Few questions are always popular. They keep on coming up through email, comments or from co-workers. Finding Last Day of Any Month is similar question. I have received it many times and I enjoy answering it as well.

I have answered this question twice before here:

SQL SERVER – Script/Function to Find Last Day of Month
SQL SERVER – Query to Find First and Last Day of Current Month

Today, we will see the same solution again. Please use the method you find appropriate to your requirement.

Following script demonstrates the script to find last day of previous, current and next month.
----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth
----Last Day of Current Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth
----Last Day of Next Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
LastDay_NextMonth

ResultSet:
LastDay_PreviousMonth
———————–
2007-07-31 23:59:59.000

LastDay_CurrentMonth
———————–
2007-08-31 23:59:59.000

LastDay_NextMonth
———————–
2007-09-30 23:59:59.000

If you want to find last day of month of any day specified use following script.
--Last Day of Any Month and Year
DECLARE @dtDate DATETIME
SET @dtDate = '8/18/2007'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
LastDay_AnyMonth

ResultSet:
LastDay_AnyMonth
———————–
2007-08-31 23:59:59.000

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL DateTime, SQL Function, SQL Scripts, SQL Utility
Previous Post
SQL SERVER – 2005 – Explanation and Script for Online Index Operations – Create, Rebuild, Drop
Next Post
SQLAuthority News – Book Review – Sams Teach Yourself Microsoft SQL Server T-SQL in 10 Minutes

Related Posts

289 Comments. Leave new

  • how to find the day of month ……..
    only enter month
    ex:month=04
    then
    find the last date of this month…..using vb.net

    Reply
  • You’re the man!
    Whenever I have a question google sends me to you…
    And you always have an answer.
    Kudos to you!

    Reply
  • I need to find the last day of the previous month based on a date entered by a user.

    Reply
  • Also, the date the user will be entering will always be the last day of some month.

    Thanks,

    Reply
  • Brian Tkatch
    June 9, 2009 6:07 pm

    @Donna

    You can subtract the amount of days in the passed date. That will give the last day of the prior month.

    DECLARE @A DATETIME
    SET @A = ’04/30/2009′
    select DATEADD(d, -DATEPART(d, @A), @A)

    Reply
  • Please help. In my stored procede, I want to assign the return value of ‘SELECT DATEADD(dd, -Day(GetDate()), GetDate())’ to a variable, e.g.

    SET @PreMonthEnd = SELECT DATEADD(dd, -Day(GetDate()), GetDate())

    What is the correct statement I should write? Thanks a lot.

    Reply
  • Atin Srivastava
    August 5, 2009 1:18 pm

    All above statements seems complicated to find last date of previous month… how abt this

    select dateadd(d,-day(getdate()),getdate())

    Reply
  • Thanks ,sunny
    It’s very useful for me …………thanks a lot

    Reply
  • thanks a lot man

    u r awesome
    thank u

    Reply
  • Most beneficial,

    Thanks

    Reply
  • what will select dateadd(s,-1,dateadd(mm,datediff(m,0,getdate())+1,0))) return?
    thanks

    Reply
  • hi there

    I need to have a bimonthly report i.e. once with 01/01/2009 to 15/01/2009
    and then 16/01/2009 to 31/01/2009

    i want to know how can i get starting and ending dates like 1 and 15 of current month if the current date is > 15 and 16 to 30/31 of the previous month if current date is < 15

    thanks a lot in advance.

    zee

    Reply
  • @zee

    WHERE date-COLUMN >= CASE
    WHEN DATEPART(dd, GETDATE()) < 16 THEN DATEADD(DD, 15, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) – 1, 0))
    ELSE DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)
    END
    AND date-COLUMN <
    CASE
    WHEN DATEPART(dd, GETDATE()) < 16 THEN DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)
    ELSE DATEADD(DD, 15, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) – 1, 0))
    END

    Reply
  • Pinal, your script worked perfectly. Thank you very much for sharing this!

    Reply
  • Thank you very much for sharing this!

    Reply
  • how to find first and last day of any month in sql?

    Reply
  • Excellent !

    the best answer I found !

    thanks

    Reply
  • SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()),1) -1

    will return last day of current month (looks shorter)

    Reply
  • Correction

    SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0) -1

    will return last day of current month (looks shorter)

    Reply
  • How do I get the Date that is 3 months prior than today? So, three months ago from today.

    PN

    Reply
    • Actually, I discovered it: dateadd(month, -3, GetDate())
      This will give date and time.
      I am working on formatting to get only the date.
      Thank you.

      Reply

Leave a Reply