Skip to content

Latest commit

 

History

History
126 lines (89 loc) · 3.21 KB

eomonth-transact-sql.md

File metadata and controls

126 lines (89 loc) · 3.21 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
EOMONTH (Transact-SQL)
Th EOMONTH function returns the last day of the month containing a specified date, with an optional offset.
markingmyname
maghan
randolphwest
05/09/2024
sql
t-sql
reference
EOMONTH
EOMONTH_TSQL
EOMONTH function
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current

EOMONTH (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw]

This function returns the last day of the month containing a specified date, with an optional offset.

Tip

In [!INCLUDE sssql22-md] and later versions, you can use DATETRUNC to calculate the start of the month.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

EOMONTH ( start_date [ , month_to_add ] )

Arguments

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 raises an error.

Return types

date

Remarks

The EOMONTH function can remote to instances running [!INCLUDE ssSQL11] and later versions. It can't remote to instances with a version before [!INCLUDE ssSQL11].

Examples

A. EOMONTH with explicit datetime type

DECLARE @date DATETIME = '12/1/2024';

SELECT EOMONTH(@date) AS Result;
GO

[!INCLUDE ssResult]

Result
------------
2024-12-31

B. EOMONTH with string parameter and implicit conversion

DECLARE @date VARCHAR(255) = '12/1/2024';

SELECT EOMONTH(@date) AS Result;
GO

[!INCLUDE ssResult]

Result
------------
2024-12-31

C. EOMONTH with and without the month_to_add parameter

The values shown in these result sets reflect an execution date between and including 12/01/2024 and 12/31/2024.

DECLARE @date DATETIME = '2024-12-31';

SELECT EOMONTH(@date) AS 'This Month';
SELECT EOMONTH(@date, 1) AS 'Next Month';
SELECT EOMONTH(@date, -1) AS 'Last Month';
GO

[!INCLUDE ssResult]

This Month
-----------------------
2024-12-31

Next Month
-----------------------
2025-01-31

Last Month
-----------------------
2024-11-30

Related content