{"id":1099,"date":"2013-06-22T03:08:37","date_gmt":"2013-06-21T21:38:37","guid":{"rendered":"http:\/\/sqlhints.com\/?p=1099"},"modified":"2013-07-07T03:10:43","modified_gmt":"2013-07-06T21:40:43","slug":"eomonth-function-in-sql-server-2012","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2013\/06\/22\/eomonth-function-in-sql-server-2012\/","title":{"rendered":"EOMONTH FUNCTION IN SQL SERVER 2012"},"content":{"rendered":"<p><strong>EOMONTH<\/strong> is one of the new built-in Date and Time function introduced as a Part of <strong>Sql Server 2012<\/strong>. It returns the last date of the Month that contains the specified date in it. <em>It is a Sql Server native function, not dependent on the .NET CLR.<\/em><\/p>\n<p><strong>SYNTAX:<\/strong> EOMONTH ( start_date [, month_to_add ] )<\/p>\n<p><strong>Parameters:<\/strong><br \/>\n<strong>start_date:<\/strong> Date Expression representing the date, for which the last date of the Month is returned<br \/>\n<strong>month_to_add<\/strong>: This is an optional integer parameter, if it is specified then specified number of months is added to the start_date and then last date of the month for the resultant date is returned.<\/p>\n<p><strong>Return Type:<\/strong> Date<\/p>\n<p><strong>Example 1: EOMONTH &#8211; start_date as DATETIME type<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS DATETIME = &#039;2012\/06\/22&#039;\r\nSELECT EOMONTH(@date) EOFMONTH<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nEOFMONTH<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n2012-06-30<\/p>\n<p><strong>Example 2: Last date of the month in previous versions of Sql Server like Sql Server 2008, 2005, 2000.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS DATETIME = &#039;2012\/06\/22&#039;\r\nSELECT DATEADD(MONTH,datediff(MONTH,-1, @date),-1) EOFMONTH<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nEOFMONTH<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2012-06-30 00:00:00.000<\/p>\n<p><strong>Example 3: EOMONTH &#8211; Start_date as valid date expression of type VARCHAR<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS varchar(10)= &#039;2012\/06\/22&#039;\r\nSELECT EOMONTH(@date) EOFMONTH<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nEOFMONTH<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n2012-06-30<\/p>\n<p><strong>Example 4: Current\/Previous\/Previou-to-Previous month&#8217;s lastdate using EOMONTH<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS DATETIME = &#039;2012\/06\/22&#039;\r\nSELECT EOMONTH(@date) &#039;Current Month&#039;,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 EOMONTH(@date, -1) &#039;Previous Month&#039;,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 EOMONTH(@date, -2) &#039;Previous-to-Previous Month&#039;<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nCurrent Month Previous Month Previous-to-Previous Month<br \/>\n&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2012-06-30 2012-05-31 2012-04-30<\/p>\n<p><strong>Example 5: Current\/Next\/Next-To-Next month&#8217;s last date using EOMONTH<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS DATETIME = &#039;2012\/06\/22&#039;\r\nSELECT EOMONTH(@date) &#039;Current Month&#039;,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 EOMONTH(@date, 1) &#039;Next Month&#039;,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 EOMONTH(@date, 2) &#039;Next-to-Next Month&#039;<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nCurrent Month Next Month Next-to-Next Month<br \/>\n&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n2012-06-30 2012-07-31 2012-08-31<\/p>\n<p><strong>Example 6: Start Date of the Month using EOMONTH<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">DECLARE @date AS DATETIME = &#039;2012\/06\/22&#039;\r\nSELECT DATEADD(DAY, 1, EOMONTH(@date, -1)) \r\n                AS &#039;Current Month Start Date&#039;<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nCurrent Month Start Date<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n2012-06-01<\/p>\n<p><strong>Example 7: Invalid start_date<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">SELECT EOMONTH(&#039;2012\/02\/31&#039;) EOFMONTH<\/pre>\n<p><span style=\"color: blue;\">Result:<\/span><br \/>\nEOFMONTH<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n<span style=\"color: red;\">Msg 241, Level 16, State 1, Line 2<br \/>\nConversion failed when converting date and\/or time from character string.<\/span><\/p>\n<p>You may like to read the below new built-in function&#8217;s introduced in Sql Server 2012:<\/p>\n<table border=\"1\">\n<tr style=\"border: 1pt solid\">\n<td  style=\"border: 1pt solid windowtext; width: 50%; text-align: center;background-color:#F43722; color:white;\" valign=\"top\" Colspan =\"2\"><strong>New Built in Functions introduced in Sql Server<\/strong><\/td>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>CONVERSION FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/parse-sql-server-2012-built-in-conversion-function\/\" title=\"PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">PARSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_parse-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_PARSE<\/a> <\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_convert-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_CONVERT Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_CONVERT<\/a><\/strong><\/td>\n<tr >\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>STRING FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/\" title=\"CONCAT String Function in Sql Server 2012\" target=\"_blank\">CONCAT<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/23\/format-string-function-in-sql-server-2012\/\" title=\"FORMAT String Function in Sql Server 2012\" target=\"_blank\">FORMAT<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>LOGICAL FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/choose-logical-function-in-sql-server-2012\/\" title=\"CHOOSE Logical Function in Sql Server 2012\" target=\"_blank\">CHOOSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/iif-logical-function-in-sql-server-2012\/\" title=\"IIF Logical Function in Sql Server 2012\" target=\"_blank\">IIF<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>DATE AND TIME FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/22\/eomonth-function-in-sql-server-2012\/\" title=\"EOMONTH Function in Sql Server 2012\" target=\"_blank\">EOMONTH<\/a><\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datefromparts-function-in-sql-server-2012\/\" title=\"DateFromParts Function in Sql Server 2012\" target=\"_blank\">DATEFROMPARTS<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datetimefromparts-function-in-sql-server-2012\/\" title=\"DateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">DATETIMEFROMPARTS<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/smalldatetimefromparts-function-in-sql-server-2012\/\" title=\"SmallDateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">SMALLDATETIMEFROMPARTS<\/a> <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIME2FROMPARTS <\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>TIMEFROMPARTS <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIMEOFFSETFROMPARTS<\/strong<\/td>\n<tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>EOMONTH is one of the new built-in Date and Time function introduced as a Part of Sql Server 2012. It returns the last date of the Month that contains the specified date in it. It is a Sql Server native function, not dependent on the .NET CLR. SYNTAX: EOMONTH ( start_date [, month_to_add ] ) &hellip; <a href=\"https:\/\/sqlhints.com\/2013\/06\/22\/eomonth-function-in-sql-server-2012\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">EOMONTH FUNCTION IN SQL SERVER 2012<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[111,13,3,5],"tags":[992,113,112,35,55,114],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-date-and-time-functions","category-functions","category-sql-server","category-sql-server-2012-sql-server","tag-date-and-time-functions","tag-end-of-month-in-sql-server-2012","tag-eofmonth","tag-new-feature-in-sql-server-2012","tag-sql-server-2012","tag-start-of-month-in-sql-server-2012"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-hJ","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":8,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1107,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/1099\/revisions\/1107"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}