EOMONTH (Transact-SQL)
返回包含指定日期的月份的最后一天(具有可选偏移量)。
语法
EOMONTH ( start_date [, month_to_add ] )
参数
start_date
日期表达式,指定要为其返回该月的最后一天的日期。month_to_add
可选的整数表达式,指定要加到 start_date 的月份数。如果指定了此参数,则 EOMONTH 将指定的月份数加到 start_date,然后返回结果日期所在月份的最后一天。 如果增加后超过有效的日期范围,则将引发错误。
返回类型
date
注释
此函数可以在 SQL Server 2012 和更高版本的服务器上远程执行。 它不能在其版本低于 SQL Server 2012 的服务器上远程执行。
示例
A.具有显式 datetime 类型的 EOMONTH
DECLARE @date DATETIME = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
下面是结果集:
Result
------------
2011-12-31
(1 row(s) affected)
B.具有字符串参数与隐式转换的 EOMONTH
DECLARE @date VARCHAR(255) = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
下面是结果集:
Result
------------
2011-12-31
(1 row(s) affected)
C.具有和不具有 month_to_add 参数的 EOMONTH
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
下面是结果集:
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)