This function returns the last day of the month containing a specified date, with an optional offset.
Transact-SQL Syntax Conventions
EOMONTH ( start_date [, month_to_add ] )
start_date
A date expression that specifies the date for which to return the last day of the month.
month_to_add
An optional integer expression that specifies the number of months to add to start_date.
If the month_to_add argument has a value, then EOMONTH
adds the specified number of months to start_date, and then returns the last day of the month for the resulting date. If this addition overflows the valid range of dates, then EOMONTH
will raise an error.
date
The EOMONTH
function can remote to SQL Server 2012 (11.x) servers and higher. It cannot be remote to servers with a version lower than SQL Server 2012 (11.x) SQL Server 2012 (11.x)
DECLARE @date DATETIME = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
Here is the result set.
Result
------------
2011-12-31
(1 row(s) affected)
DECLARE @date VARCHAR(255) = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
Here is the result set.
Result
------------
2011-12-31
(1 row(s) affected)
Note: the values shown in these result sets reflect an execution date between and including
12/01/2011
and
12/31/2011
DECLARE @date DATETIME = GETDATE();
SELECT EOMONTH ( @date ) AS 'This Month';
SELECT EOMONTH ( @date, 1 ) AS 'Next Month';
SELECT EOMONTH ( @date, -1 ) AS 'Last Month';
GO
Here is the result set.
This Month
-----------------------
2011-12-31
(1 row(s) affected)
Next Month
-----------------------
2012-01-31
(1 row(s) affected)
Last Month
-----------------------
2011-11-30
(1 row(s) affected)