EOMONTH (Transact-SQL)EOMONTH (Transact-SQL)

適用於: 是SQL Server (從 2012 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2012) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

此函式會以選擇性位移,傳回包含指定日期的當月最後一天。This function returns the last day of the month containing a specified date, with an optional offset.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

EOMONTH ( start_date [, month_to_add ] )  

引數Arguments

start_datestart_date
日期運算式,指定要傳回當月最後一天的日期。A date expression that specifies the date for which to return the last day of the month.

month_to_addmonth_to_add
選擇性整數運算式,指定要新增至 start_date 的月數。An optional integer expression that specifies the number of months to add to start_date.

如果 month_to_add 引數具有值,則 EOMONTH 會將指定月數新增至 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. 如果這個加法溢位有效日期範圍,則 EOMONTH 會引發錯誤。If this addition overflows the valid range of dates, then EOMONTH will raise an error.

傳回類型Return Type

datedate

RemarksRemarks

EOMONTH 函式可以遠端處理到 SQL Server 2012 (11.x)SQL Server 2012 (11.x) 伺服器和更新版本。The EOMONTH function can remote to SQL Server 2012 (11.x)SQL Server 2012 (11.x) servers and higher. 它無法遠端處理到版本低於 SQL Server 2012 (11.x)SQL Server 2012 (11.x) 的伺服器。It cannot be remote to servers with a version lower than SQL Server 2012 (11.x)SQL Server 2012 (11.x).

範例Examples

A.A. 具明確日期時間類型的 EOMONTHEOMONTH with explicit datetime type

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)  

B.B. 具有字串參數及明確轉換的 EOMONTHEOMONTH with string parameter and implicit conversion

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)  

C.C. 具有和不具有 month_to_add 參數的 EOMONTHEOMONTH with and without the month_to_add parameter

注意:這些結果集中顯示的值反映之間的執行日期,包括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)