Find Last Day Of a Month in SQL Server 2005

Let us see how to find out the last day of a month :

-- Return Last Date of a Month
DECLARE @Month smallint
SET @Month = 1
SELECT DATEADD(Month, DATEDIFF(Month,0,getdate())+@Month, -1) as LastDateOfMonth,
DAY(DATEADD(Month, DATEDIFF(Month,0,getdate())+@Month, -1)) as LastDayOfMonth

The @Month accepts the month that needs to be added as the third parameter of the DATEADD function. You can set different values as shown below to view the last day of the month

1 - Current Month
0 - Last Month
2 - Next Month

1 comment:

  1. That function does not return the correct values at all.

    ReplyDelete