Skip to content

Latest commit

 

History

History
194 lines (142 loc) · 4.28 KB

File metadata and controls

194 lines (142 loc) · 4.28 KB
title EOMONTH (Transact-SQL) | Microsoft Docs
ms.custom
ms.date 03/04/2017
ms.prod sql-non-specified
ms.reviewer
ms.suite
ms.technology
database-engine
ms.tgt_pltfrm
ms.topic language-reference
f1_keywords
EOMONTH
EOMONTH_TSQL
dev_langs
TSQL
helpviewer_keywords
EOMONTH function
ms.assetid 1d060d8e-3297-4244-afef-57df2f8f92e2
caps.latest.revision 19
author BYHAM
ms.author rickbyh
manager jhubbard

EOMONTH (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2012-all_md]

Returns the last day of the month that contains the specified date, with an optional offset.

Topic link icon Transact-SQL Syntax Conventions

Syntax

EOMONTH ( start_date [, month_to_add ] )  

Arguments

start_date
Date expression specifying the date for which to return the last day of the month.

month_to_add
Optional integer expression specifying the number of months to add to start_date.

If this argument is specified, 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 an error is raised.

Return Type

date

Remarks

This function can be remoted to [!INCLUDEssSQL11] servers and higher. It cannot be remoted to servers with a version lower than [!INCLUDEssSQL11].

Examples

A. EOMONTH with explicit datetime type

DECLARE @date DATETIME = '12/1/2011';  
SELECT EOMONTH ( @date ) AS Result;  
GO  

[!INCLUDEssResult]

Result  
------------  
2011-12-31  
  
(1 row(s) affected)  

B. EOMONTH with string parameter and implicit conversion

DECLARE @date VARCHAR(255) = '12/1/2011';  
SELECT EOMONTH ( @date ) AS Result;  
GO  

[!INCLUDEssResult]

Result  
------------  
2011-12-31  
  
(1 row(s) affected)  

C. EOMONTH with and without the month_to_add parameter

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  

[!INCLUDEssResult]

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)  

Examples: [!INCLUDEssSDWfull] and [!INCLUDEssPDW]

D. EOMONTH with explicit datetime type

DECLARE @date DATETIME = '12/1/2011';  
SELECT EOMONTH ( @date ) AS Result;  
GO  

[!INCLUDEssResult]

Result  
------------  
2011-12-31  
  
(1 row(s) affected)  

E. EOMONTH with string parameter and implicit conversion

DECLARE @date VARCHAR(255) = '12/1/2011';  
SELECT EOMONTH ( @date ) AS Result;  
GO  

[!INCLUDEssResult]

Result  
------------  
2011-12-31  
  
(1 row(s) affected)  

F. EOMONTH with and without the month_to_add parameter

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  

[!INCLUDEssResult]

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)