Cari Blog Ini

02 Maret 2012

Script or Function to Find Last Day of Month

Script:

DECLARE @date DATETIME 
 SET @date='2008-02-03' 
 SELECT DATEADD(dd, -DAY(DATEADD(m,1,@date)), DATEADD(m,1,@date))
            AS LastDayOfMonth
GO 
 
DECLARE @date DATETIME 
 SET @date='2007-02-03' 
 SELECT DATEADD(dd, -DAY(DATEADD(m,1,@date)), DATEADD(m,1,@date))
            AS LastDayOfMonth
GO
 
 
ResultSet:
LastDayOfMonth
-----------------------
2008-02-29 00:00:00.000

(1 row(s) affected)

LastDayOfMonth
-----------------------
2007-02-28 00:00:00.000

(1 row(s) affected)
 
 
Source:
http://blog.sqlauthority.com/2007/05/20/sql-server-scriptfunction-to-find-last-day-of-month/