|
1 | | ---- |
2 | | -title: "EOMONTH (Transact-SQL)" |
3 | | -description: "EOMONTH (Transact-SQL)" |
4 | | -author: markingmyname |
5 | | -ms.author: maghan |
6 | | -ms.date: "03/04/2017" |
7 | | -ms.service: sql |
8 | | -ms.subservice: t-sql |
9 | | -ms.topic: reference |
10 | | -f1_keywords: |
11 | | - - "EOMONTH" |
12 | | - - "EOMONTH_TSQL" |
13 | | -helpviewer_keywords: |
14 | | - - "EOMONTH function" |
15 | | -dev_langs: |
16 | | - - "TSQL" |
17 | | -monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current" |
18 | | ---- |
19 | | -# EOMONTH (Transact-SQL) |
20 | | -[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)] |
21 | | - |
22 | | -This function returns the last day of the month containing a specified date, with an optional offset. |
23 | | - |
24 | | - :::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) |
25 | | - |
26 | | -## Syntax |
27 | | - |
28 | | -```syntaxsql |
29 | | -EOMONTH ( start_date [, month_to_add ] ) |
30 | | -``` |
31 | | - |
32 | | -[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)] |
33 | | - |
34 | | -## Arguments |
35 | | -*start_date* |
36 | | -A date expression that specifies the date for which to return the last day of the month. |
37 | | - |
38 | | -*month_to_add* |
39 | | -An optional integer expression that specifies the number of months to add to *start_date*. |
40 | | - |
41 | | -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. |
42 | | - |
43 | | -## Return Type |
44 | | - **date** |
45 | | - |
46 | | -## Remarks |
47 | | -The `EOMONTH` function can remote to [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)] servers and higher. It cannot be remote to servers with a version lower than [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)]. |
48 | | - |
49 | | -Use `DATETRUNC(MONTH, @date)` to calculate start of month "SOMONTH". See [DATETRUNC](./datetrunc-transact-sql.md). |
50 | | - |
51 | | -## Examples |
52 | | - |
53 | | -### A. EOMONTH with explicit datetime type |
54 | | - |
55 | | -```sql |
56 | | -DECLARE @date DATETIME = '12/1/2011'; |
57 | | -SELECT EOMONTH ( @date ) AS Result; |
58 | | -GO |
59 | | -``` |
60 | | - |
61 | | -[!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
62 | | - |
63 | | -``` |
64 | | -Result |
65 | | ------------- |
66 | | -2011-12-31 |
67 | | - |
68 | | -(1 row(s) affected) |
69 | | -``` |
70 | | - |
71 | | -### B. EOMONTH with string parameter and implicit conversion |
72 | | - |
73 | | -```sql |
74 | | -DECLARE @date VARCHAR(255) = '12/1/2011'; |
75 | | -SELECT EOMONTH ( @date ) AS Result; |
76 | | -GO |
77 | | -``` |
78 | | - |
79 | | -[!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
80 | | - |
81 | | -``` |
82 | | -Result |
83 | | ------------- |
84 | | -2011-12-31 |
85 | | - |
86 | | -(1 row(s) affected) |
87 | | -``` |
88 | | - |
89 | | -### C. EOMONTH with and without the month_to_add parameter |
90 | | - |
91 | | -The values shown in these result sets reflect an execution date between and including `12/01/2011` and `12/31/2011`. |
92 | | - |
93 | | -```sql |
94 | | -DECLARE @date DATETIME = GETDATE(); |
95 | | -SELECT EOMONTH ( @date ) AS 'This Month'; |
96 | | -SELECT EOMONTH ( @date, 1 ) AS 'Next Month'; |
97 | | -SELECT EOMONTH ( @date, -1 ) AS 'Last Month'; |
98 | | -GO |
99 | | -``` |
100 | | - |
101 | | - [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
102 | | - |
103 | | -``` |
104 | | -This Month |
105 | | ------------------------ |
106 | | -2011-12-31 |
107 | | - |
108 | | -(1 row(s) affected) |
109 | | - |
110 | | -Next Month |
111 | | ------------------------ |
112 | | -2012-01-31 |
113 | | - |
114 | | -(1 row(s) affected) |
115 | | - |
116 | | -Last Month |
117 | | ------------------------ |
118 | | -2011-11-30 |
119 | | - |
120 | | -(1 row(s) affected) |
121 | | -``` |
| 1 | +--- |
| 2 | +title: "EOMONTH (Transact-SQL)" |
| 3 | +description: Th EOMONTH function returns the last day of the month containing a specified date, with an optional offset. |
| 4 | +author: markingmyname |
| 5 | +ms.author: maghan |
| 6 | +ms.reviewer: randolphwest |
| 7 | +ms.date: 11/02/2023 |
| 8 | +ms.service: sql |
| 9 | +ms.subservice: t-sql |
| 10 | +ms.topic: reference |
| 11 | +f1_keywords: |
| 12 | + - "EOMONTH" |
| 13 | + - "EOMONTH_TSQL" |
| 14 | +helpviewer_keywords: |
| 15 | + - "EOMONTH function" |
| 16 | +dev_langs: |
| 17 | + - "TSQL" |
| 18 | +monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current" |
| 19 | +--- |
| 20 | +# EOMONTH (Transact-SQL) |
| 21 | + |
| 22 | +[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)] |
| 23 | + |
| 24 | +This function returns the last day of the month containing a specified date, with an optional offset. |
| 25 | + |
| 26 | +> [!TIP] |
| 27 | +> You can use [DATETRUNC](./datetrunc-transact-sql.md) to calculate the start of the month. |
| 28 | +
|
| 29 | +:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) |
| 30 | + |
| 31 | +## Syntax |
| 32 | + |
| 33 | +```syntaxsql |
| 34 | +EOMONTH ( start_date [ , month_to_add ] ) |
| 35 | +``` |
| 36 | + |
| 37 | +[!INCLUDE [sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)] |
| 38 | + |
| 39 | +## Arguments |
| 40 | + |
| 41 | +#### *start_date* |
| 42 | + |
| 43 | +A date expression that specifies the date for which to return the last day of the month. |
| 44 | + |
| 45 | +#### *month_to_add* |
| 46 | + |
| 47 | +An optional integer expression that specifies the number of months to add to *start_date*. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +## Return types |
| 52 | + |
| 53 | +**date** |
| 54 | + |
| 55 | +## Remarks |
| 56 | + |
| 57 | +The `EOMONTH` function can remote to instances running [!INCLUDE [ssSQL11](../../includes/sssql11-md.md)] and later versions. It can't remote to instances with a version before [!INCLUDE [ssSQL11](../../includes/sssql11-md.md)]. |
| 58 | + |
| 59 | +## Examples |
| 60 | + |
| 61 | +### A. EOMONTH with explicit datetime type |
| 62 | + |
| 63 | +```sql |
| 64 | +DECLARE @date DATETIME = '12/1/2022'; |
| 65 | +SELECT EOMONTH ( @date ) AS Result; |
| 66 | +GO |
| 67 | +``` |
| 68 | + |
| 69 | +[!INCLUDE [ssResult](../../includes/ssresult-md.md)] |
| 70 | + |
| 71 | +```output |
| 72 | +Result |
| 73 | +------------ |
| 74 | +2022-12-31 |
| 75 | +
|
| 76 | +(1 row(s) affected) |
| 77 | +``` |
| 78 | + |
| 79 | +### B. EOMONTH with string parameter and implicit conversion |
| 80 | + |
| 81 | +```sql |
| 82 | +DECLARE @date VARCHAR(255) = '12/1/2022'; |
| 83 | +SELECT EOMONTH ( @date ) AS Result; |
| 84 | +GO |
| 85 | +``` |
| 86 | + |
| 87 | +[!INCLUDE [ssResult](../../includes/ssresult-md.md)] |
| 88 | + |
| 89 | +```output |
| 90 | +Result |
| 91 | +------------ |
| 92 | +2022-12-31 |
| 93 | +
|
| 94 | +(1 row(s) affected) |
| 95 | +``` |
| 96 | + |
| 97 | +### C. EOMONTH with and without the month_to_add parameter |
| 98 | + |
| 99 | +The values shown in these result sets reflect an execution date between and including `12/01/2022` and `12/31/2022`. |
| 100 | + |
| 101 | +```sql |
| 102 | +DECLARE @date DATETIME = GETDATE(); |
| 103 | +SELECT EOMONTH ( @date ) AS 'This Month'; |
| 104 | +SELECT EOMONTH ( @date, 1 ) AS 'Next Month'; |
| 105 | +SELECT EOMONTH ( @date, -1 ) AS 'Last Month'; |
| 106 | +GO |
| 107 | +``` |
| 108 | + |
| 109 | +[!INCLUDE [ssResult](../../includes/ssresult-md.md)] |
| 110 | + |
| 111 | +```output |
| 112 | +This Month |
| 113 | +----------------------- |
| 114 | +2022-12-31 |
| 115 | +
|
| 116 | +(1 row(s) affected) |
| 117 | +
|
| 118 | +Next Month |
| 119 | +----------------------- |
| 120 | +2012-01-31 |
| 121 | +
|
| 122 | +(1 row(s) affected) |
| 123 | +
|
| 124 | +Last Month |
| 125 | +----------------------- |
| 126 | +2022-11-30 |
| 127 | +
|
| 128 | +(1 row(s) affected) |
| 129 | +``` |
0 commit comments