Monday, June 4, 2007

SQL Statement Last Day of a month

SQL Statement below show the Sql query to get the last day of a month value by using sql DATEADD.
Example of SQL Statement to get last day of a month


DECLARE @DateNow DATETIME
SET @DateNow='2007-07-04'
SELECT DATEADD(dd, -DAY(DATEADD(m,1,@DateNow)), DATEADD(m,1,@DateNow)) AS LastDay

Return Value = 2007-07-31 00:00:00.000



DECLARE @DateNow DATETIME
SET @DateNow='2007-06-04'
SELECT DATEADD(dd, -DAY(DATEADD(m,1,@DateNow)), DATEADD(m,1,@DateNow)) AS LastDay

Return Value = 2007-06-30 00:00:00.000