Find Last Day of the Month

  • Comments posted to this topic are about the item Find Last Day of the Month

  • Another way to do this :

    1) Add 1 month

    2) Substract the day number of the previous result :

    DECLARE @mydate datetime='20080224'

    SELECT DATEADD(day, -DATEPART(day, @Mydate), DATEADD(MONTH, 1, @mydate))

  • f.marquis (5/10/2011)


    Another way to do this :

    1) Add 1 month

    2) Substract the day number of the previous result :

    DECLARE @mydate datetime='20080224'

    SELECT DATEADD(day, -DATEPART(day, @Mydate), DATEADD(MONTH, 1, @mydate))

    One problem with this is... it doesnt work. Run the code for '20080331' and it returns '20080330'.

    /T

  • You're right :

    I think I wrote the good instructions but a wrong SQL.

    This might be better :

    1) Add 1 month

    2) Substract the day number of the previousresult :

    DECLARE @mydate datetime='20080331'

    SELECT DATEADD( day, -DATEPART(day, DATEADD(MONTH, 1, @mydate)), DATEADD(MONTH, 1, @mydate))

  • A quick search on this site and one could for instance find

    http://www.sqlservercentral.com/Forums/Topic777289-1291-1.aspx

    Scroll down to Lynn Pettis first post. I would recommend reading the whole post since there is also some interesting posts on the following pages.

    /T

  • Below is a one line statement to compute last date of the month for a given input date.

    DECLARE @MyDate datetime

    SET @MyDate = '2012/11/25'

    SELECT DATEADD(DAY,DATEPART(DAY,DATEADD(MONTH,1,@MyDate))*-1,DATEADD(MONTH,1,@MyDate))

    --Output result: 2012-11-30 00:00:00.000

  • select dateadd(d,-1,left(CONVERT(varchar,CURRENT_TIMESTAMP,112),6)+'01')

  • good one!

  • This one will do the trick with just three instructions (and no string conversions which are best avoided):

    SELECT

    "LastDayOfMonth" = DatePart(day, DateAdd(month, DateDiff(month, -1, GetDate()), -1))

  • I prefer this statement

    DECLARE @myDate DATETIME

    SELECT @myDate = GETDATE()

    SELECT DATEADD(dd, -(DAY(DATEADD(mm, 1, @myDate))), DATEADD(mm, 1, @myDate))

    And then to get the date without the time just plug it in here:

    SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @myDate))



    The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.

  • Use this to get the last day of the month

    DECLARE @date DATETIME

    SELECT @date='2011/05/10'

    SELECT DATEADD(DD, -1, DATEADD(MM, DATEDIFF(MM,0,@date)+1,0))

  • wallah!???

    I think you mean voilà

    A wallah is a servant...

  • SELECT DATEADD(MM,DATEDIFF(MM,-1,getdate()),0)-1

    Regards,
    Mitesh OSwal
    +918698619998

  • --Last day of the current month.

    --You could format it and it also takes into account the timestamp - 3 milliseconds

    SELECT DATEADD(MS,-3,DATEADD(MM, DATEDIFF(M,0,GETDATE() )+1, 0))

  • DateTime already has this implemented...

    DateTime.DaysInMonth(Year as Int32, Month as Int32)

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply