{"id":1156,"date":"2025-05-01T18:33:10","date_gmt":"2025-05-01T18:33:10","guid":{"rendered":"https:\/\/kourentzes.com\/konstantinos\/?p=1156"},"modified":"2025-05-01T18:34:13","modified_gmt":"2025-05-01T18:34:13","slug":"emulating-eomonth-in-legacy-sql-server-versions","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/","title":{"rendered":"Emulating EOMONTH in Legacy SQL Server Versions"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>EOMONTH()<\/strong>:<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT EOMONTH(DATEFROMPARTS(2025, 4, 1)) AS EndOfMonthDate\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F78C6C\">SELECT<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">EOMONTH<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #82AAFF\">DATEFROMPARTS<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #F78C6C\">2025<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #F78C6C\">4<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #EEFFFF\">)) <\/span><span style=\"color: #F78C6C\">AS<\/span><span style=\"color: #EEFFFF\"> EndOfMonthDate<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">SQL<\/span><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code> Output: 2025-04-30<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SQL Server 2012+ offers <code>EOMONTH()<\/code> for retrieving the end of any given month.<\/li>\n\n\n\n<li>Legacy versions (SQL Server 2005\/2008) lack this built-in function.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s create a user-defined scalar function <code>dbo.fn_EndOfMonth<\/code> to compute the last day of any month using date manipulation techniques.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Methodology: <\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Builds the <strong>first day of the next month<\/strong> from the input year and month.<\/li>\n\n\n\n<li><strong>Subtracts one day<\/strong> to get the last day of the target month.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"CREATE FUNCTION dbo.fn_EndOfMonth (@Year INT, @Month INT)\nRETURNS DATE\nAS\nBEGIN\n    DECLARE @FirstOfNextMonth DATE\n    DECLARE @EndOfMonth DATE\n\n    -- Step 1: Get the 1st day of the next month\n    SET @FirstOfNextMonth = DATEADD(MONTH, 1, CAST(CONCAT(@Year, RIGHT('0' + CAST(@Month AS VARCHAR(2)), 2), '01') AS DATE))\n\n    -- Step 2: Subtract 1 day to get last day of target month\n    SET @EndOfMonth = DATEADD(DAY, -1, @FirstOfNextMonth)\n\n    RETURN @EndOfMonth\nEND\n\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F78C6C\">CREATE<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">FUNCTION<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">dbo<\/span><span style=\"color: #EEFFFF\">.fn_EndOfMonth (@Year <\/span><span style=\"color: #C792EA\">INT<\/span><span style=\"color: #EEFFFF\">, @Month <\/span><span style=\"color: #C792EA\">INT<\/span><span style=\"color: #EEFFFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F78C6C\">RETURNS<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C792EA\">DATE<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F78C6C\">AS<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F78C6C\">BEGIN<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #F78C6C\">DECLARE<\/span><span style=\"color: #EEFFFF\"> @FirstOfNextMonth <\/span><span style=\"color: #C792EA\">DATE<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #F78C6C\">DECLARE<\/span><span style=\"color: #EEFFFF\"> @EndOfMonth <\/span><span style=\"color: #C792EA\">DATE<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF\">    <\/span><span style=\"color: #545454; font-style: italic\">-- Step 1: Get the 1st day of the next month<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #F78C6C\">SET<\/span><span style=\"color: #EEFFFF\"> @FirstOfNextMonth <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">DATEADD<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #F78C6C\">MONTH<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #82AAFF\">CAST<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #82AAFF\">CONCAT<\/span><span style=\"color: #EEFFFF\">(@Year, <\/span><span style=\"color: #82AAFF\">RIGHT<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #C3E88D\">0<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">+<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">CAST<\/span><span style=\"color: #EEFFFF\">(@Month <\/span><span style=\"color: #F78C6C\">AS<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C792EA\">VARCHAR<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #F78C6C\">2<\/span><span style=\"color: #EEFFFF\">)), <\/span><span style=\"color: #F78C6C\">2<\/span><span style=\"color: #EEFFFF\">), <\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #C3E88D\">01<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #EEFFFF\">) <\/span><span style=\"color: #F78C6C\">AS<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C792EA\">DATE<\/span><span style=\"color: #EEFFFF\">))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF\">    <\/span><span style=\"color: #545454; font-style: italic\">-- Step 2: Subtract 1 day to get last day of target month<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #F78C6C\">SET<\/span><span style=\"color: #EEFFFF\"> @EndOfMonth <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">DATEADD<\/span><span style=\"color: #EEFFFF\">(<\/span><span style=\"color: #F78C6C\">DAY<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #89DDFF\">-<\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #EEFFFF\">, @FirstOfNextMonth)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #F78C6C\">RETURN<\/span><span style=\"color: #EEFFFF\"> @EndOfMonth<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F78C6C\">END<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">SQL<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT dbo.fn_EndOfMonth(2025, 4) AS EndOfMonth\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F78C6C\">SELECT<\/span><span style=\"color: #EEFFFF\"> dbo.fn_EndOfMonth(<\/span><span style=\"color: #F78C6C\">2025<\/span><span style=\"color: #EEFFFF\">, <\/span><span style=\"color: #F78C6C\">4<\/span><span style=\"color: #EEFFFF\">) <\/span><span style=\"color: #F78C6C\">AS<\/span><span style=\"color: #EEFFFF\"> EndOfMonth<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">SQL<\/span><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>Output: 2025-04-30<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.<\/p>\n","protected":false},"author":1,"featured_media":1158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,863],"tags":[862,861,855,856,859,854,853,143,858,857,42,860],"class_list":["post-1156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code-snipperts","category-t-sql","tag-backward-compatibility","tag-database-compatibility","tag-date-calculations","tag-end-of-month","tag-eomonth","tag-legacy-sql","tag-sql-function","tag-sql-server","tag-sql-server-2005","tag-sql-server-2008","tag-t-sql","tag-user-defined-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com<\/title>\n<meta name=\"description\" content=\"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com\" \/>\n<meta property=\"og:description\" content=\"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/\" \/>\n<meta property=\"og:site_name\" content=\"konstantinos.kourentzes.com\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-01T18:33:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-01T18:34:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Konstantinos Kourentzes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@kkourentzes\" \/>\n<meta name=\"twitter:site\" content=\"@kkourentzes\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Konstantinos Kourentzes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/\"},\"author\":{\"name\":\"Konstantinos Kourentzes\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"headline\":\"Emulating EOMONTH in Legacy SQL Server Versions\",\"datePublished\":\"2025-05-01T18:33:10+00:00\",\"dateModified\":\"2025-05-01T18:34:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/\"},\"wordCount\":77,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"image\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png\",\"keywords\":[\"Backward Compatibility\",\"Database Compatibility\",\"Date Calculations\",\"End of Month\",\"EOMONTH\",\"Legacy SQL\",\"SQL Function\",\"SQL Server\",\"SQL Server 2005\",\"SQL Server 2008\",\"t-sql\",\"User Defined Function\"],\"articleSection\":[\"Code Snippets\",\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/\",\"name\":\"Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png\",\"datePublished\":\"2025-05-01T18:33:10+00:00\",\"dateModified\":\"2025-05-01T18:34:13+00:00\",\"description\":\"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png\",\"contentUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png\",\"width\":1024,\"height\":1024,\"caption\":\"Emulating EOMONTH in Legacy SQL Server Versions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/05\\\/01\\\/emulating-eomonth-in-legacy-sql-server-versions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Emulating EOMONTH in Legacy SQL Server Versions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#website\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/\",\"name\":\"kourentzes.com\\\/konstantinos\",\"description\":\"Konstantinos Kourentzes\",\"publisher\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\",\"name\":\"Konstantinos Kourentzes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"contentUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"width\":2835,\"height\":2268,\"caption\":\"Konstantinos Kourentzes\"},\"logo\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\"},\"description\":\"Konstantinos Kourentzes is a distinguished technologist and Enterprise Resource Planning (ERP) consultant renowned for his expertise in delivering cutting-edge technology solutions. Based in Marousi, Greece, he has a knack for seamlessly integrating data-driven systems, empowering businesses to streamline their operations and achieve peak efficiency. A fervent proponent of innovation, Konstantinos is committed to instigating revolutionary shifts within organizations. His approach revolves around delivering custom-tailored ERP solutions that seamlessly align with each business's distinctive requirements. This catalyzes enduring collaborations rooted in unwavering trust and tangible outcomes. With a background rooted in technology and a passion for optimizing business processes, Konstantinos is your go-to partner for harnessing the power of ERP systems to unlock operational excellence. Connect with Konstantinos on LinkedIn to explore how his technological insights can drive your business to new heights.\",\"sameAs\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\",\"https:\\\/\\\/x.com\\\/kkourentzes\"],\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/author\\\/administrator\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com","description":"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/","og_locale":"en_US","og_type":"article","og_title":"Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com","og_description":"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.","og_url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/","og_site_name":"konstantinos.kourentzes.com","article_published_time":"2025-05-01T18:33:10+00:00","article_modified_time":"2025-05-01T18:34:13+00:00","og_image":[{"width":1024,"height":1024,"url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png","type":"image\/png"}],"author":"Konstantinos Kourentzes","twitter_card":"summary_large_image","twitter_creator":"@kkourentzes","twitter_site":"@kkourentzes","twitter_misc":{"Written by":"Konstantinos Kourentzes","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#article","isPartOf":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/"},"author":{"name":"Konstantinos Kourentzes","@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"headline":"Emulating EOMONTH in Legacy SQL Server Versions","datePublished":"2025-05-01T18:33:10+00:00","dateModified":"2025-05-01T18:34:13+00:00","mainEntityOfPage":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/"},"wordCount":77,"commentCount":0,"publisher":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"image":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#primaryimage"},"thumbnailUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png","keywords":["Backward Compatibility","Database Compatibility","Date Calculations","End of Month","EOMONTH","Legacy SQL","SQL Function","SQL Server","SQL Server 2005","SQL Server 2008","t-sql","User Defined Function"],"articleSection":["Code Snippets","T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/","url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/","name":"Emulating EOMONTH in Legacy SQL Server Versions - konstantinos.kourentzes.com","isPartOf":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#primaryimage"},"image":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#primaryimage"},"thumbnailUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png","datePublished":"2025-05-01T18:33:10+00:00","dateModified":"2025-05-01T18:34:13+00:00","description":"In environments running legacy versions of SQL Server that lack the EOMONTH() function, calculating the last date of a month requires a manual workaround. This article introduces a concise and reliable T-SQL user-defined function fn_EndOfMonth, which computes the end-of-month date for any given year and month. The function constructs the first day of the next month and subtracts one day to derive the correct result. It is compatible with SQL Server 2005, 2008, and 2008 R2, making it ideal for teams maintaining older databases. Code examples and usage scenarios are provided to help developers adopt the solution seamlessly.","breadcrumb":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#primaryimage","url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png","contentUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/05\/emperor_kk_Emulating_EOMONTH_in_Legacy_SQL_Server_Versions_-_e7c22179-f940-4842-bb2c-ce1061e8ae1d_3.png","width":1024,"height":1024,"caption":"Emulating EOMONTH in Legacy SQL Server Versions"},{"@type":"BreadcrumbList","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/05\/01\/emulating-eomonth-in-legacy-sql-server-versions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kourentzes.com\/konstantinos\/"},{"@type":"ListItem","position":2,"name":"Emulating EOMONTH in Legacy SQL Server Versions"}]},{"@type":"WebSite","@id":"https:\/\/kourentzes.com\/konstantinos\/#website","url":"https:\/\/kourentzes.com\/konstantinos\/","name":"kourentzes.com\/konstantinos","description":"Konstantinos Kourentzes","publisher":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kourentzes.com\/konstantinos\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822","name":"Konstantinos Kourentzes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","contentUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","width":2835,"height":2268,"caption":"Konstantinos Kourentzes"},"logo":{"@id":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png"},"description":"Konstantinos Kourentzes is a distinguished technologist and Enterprise Resource Planning (ERP) consultant renowned for his expertise in delivering cutting-edge technology solutions. Based in Marousi, Greece, he has a knack for seamlessly integrating data-driven systems, empowering businesses to streamline their operations and achieve peak efficiency. A fervent proponent of innovation, Konstantinos is committed to instigating revolutionary shifts within organizations. His approach revolves around delivering custom-tailored ERP solutions that seamlessly align with each business's distinctive requirements. This catalyzes enduring collaborations rooted in unwavering trust and tangible outcomes. With a background rooted in technology and a passion for optimizing business processes, Konstantinos is your go-to partner for harnessing the power of ERP systems to unlock operational excellence. Connect with Konstantinos on LinkedIn to explore how his technological insights can drive your business to new heights.","sameAs":["https:\/\/kourentzes.com\/konstantinos","https:\/\/x.com\/kkourentzes"],"url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/author\/administrator\/"}]}},"_links":{"self":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/1156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/comments?post=1156"}],"version-history":[{"count":1,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/1156\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/1156\/revisions\/1157"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/media\/1158"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/media?parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/categories?post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/tags?post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}