{"id":81,"date":"2012-01-06T13:18:08","date_gmt":"2012-01-06T13:18:08","guid":{"rendered":"http:\/\/bicortex.com\/?p=81"},"modified":"2012-01-12T12:03:52","modified_gmt":"2012-01-12T12:03:52","slug":"sql-calculating-number-of-days-in-a-month","status":"publish","type":"post","link":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/","title":{"rendered":"SQL &#8211; Calculating number of days in a month"},"content":{"rendered":"<p style=\"text-align: justify;\">Here is a little function which I found myself using a number of times (particularly to calculate averages over the length of a specific month) which calculates number of days in any given month. All you need to do, is to provider it with a datetime input parameter to return number of days as an integer.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSET ANSI_NULLS ON\r\nGO\r\nSET QUOTED_IDENTIFIER ON\r\nGO\r\n\r\nCREATE FUNCTION &#x5B;dbo].&#x5B;fun_GetDaysInMonth] (@pDate DATETIME )\r\nRETURNS INT\r\nAS\r\nBEGIN\r\n    RETURN CASE WHEN MONTH(@pDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31\r\n                WHEN MONTH(@pDate) IN (4, 6, 9, 11) THEN 30\r\n                ELSE CASE WHEN (YEAR(@pDate) % 4    = 0 AND\r\n                                YEAR(@pDate) % 100 != 0) OR\r\n                               (YEAR(@pDate) % 400  = 0)\r\n                          THEN 29\r\n                          ELSE 28\r\n                     END\r\n           END\r\nEND\r\nGO\r\n<\/pre>\n<p style=\"text-align: justify;\">And here is the SQL code and results from the actual implementation. All we need to do is call the function, here using &#8216;GETDATE()&#8217; as an input parameter and repeating it a few times using &#8216;DATEADD&#8217; function to generate few more months in a row.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n------------------------------------------------------------------------\r\n--\t  \t\t\t\t\tDAYS IN JANUARY 2012\t\t\t\t\t\t\t|\r\n------------------------------------------------------------------------\r\nSELECT convert (nvarchar (10), datename(month,'2012\/01\/01')) + ' '\r\n+ convert (nvarchar (10), datepart(year,'2012\/01\/01')) as &#x5B;Month\/Year],\r\ndbo.fun_GetDaysInMonth (GETDATE()) as Number_of_Days;\r\n\r\n------------------------------------------------------------------------\r\n--\t\t\t\t\t\tDAYS IN FEBRUARY 2012\t\t\t\t\t\t\t|\r\n------------------------------------------------------------------------\r\nSELECT convert (nvarchar (10), datename(month,'2012\/02\/01')) + ' '\r\n+ convert (nvarchar (10), datepart(year,'2012\/02\/01')) as &#x5B;Month\/Year],\r\ndbo.fun_GetDaysInMonth (dateadd(month,1,GETDATE())) as Number_of_Days;\r\n\r\n------------------------------------------------------------------------\r\n--\t\t\t\t\t\tDAYS IN MARCH 2012\t\t\t\t\t\t\t\t|\r\n------------------------------------------------------------------------\r\nSELECT convert (nvarchar (10), datename(month,'2012\/03\/01')) + ' '\r\n+ convert (nvarchar (10), datepart(year,'2012\/03\/01')) as &#x5B;Month\/Year],\r\ndbo.fun_GetDaysInMonth (dateadd(month,2,GETDATE())) as Number_of_Days;\r\n\r\n------------------------------------------------------------------------\r\n--\t\t\t\t\t\tDAYS IN APRIL 2012\t\t\t\t\t\t\t\t|\r\n------------------------------------------------------------------------\r\nSELECT convert (nvarchar (10), datename(month,'2012\/04\/01')) + ' '\r\n+ convert (nvarchar (10), datepart(year,'2012\/04\/01')) as &#x5B;Month\/Year],\r\ndbo.fun_GetDaysInMonth (dateadd(month,3,GETDATE())) as Number_of_Days;\r\nGO\r\n<\/pre>\n<p style=\"text-align: center;\"><a href=\"http:\/\/bicortex.com\/sql-calculating-number-of-days-in-a-month\/sql_results_fun_getdaysinmonth\/\" rel=\"attachment wp-att-170\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-170\" style=\"border-width: 0px; border-color: currentColor; border-style: none;\" title=\"SQL_Results_fun_GetDaysInMonth\" src=\"http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/\/2012\/01\/SQL_Results_fun_GetDaysInMonth.png\" alt=\"\" width=\"377\" height=\"205\" srcset=\"http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/2012\/01\/SQL_Results_fun_GetDaysInMonth.png 377w, http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/2012\/01\/SQL_Results_fun_GetDaysInMonth-300x163.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a little function which I found myself using a number of times (particularly to calculate averages over the length of a specific month) which calculates number of days in any given month. All you need to do, is to provider it with a datetime input parameter to return number of days as an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[12,49],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-sql","tag-code","tag-sql"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"admin\"\/>\n\t<link rel=\"canonical\" href=\"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"bicortex | Business Intelligence &amp; Analytics\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"SQL \u2013 Calculating number of days in a month | bicortex\" \/>\n\t\t<meta property=\"og:url\" content=\"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2012-01-06T13:18:08+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2012-01-12T12:03:52+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"SQL \u2013 Calculating number of days in a month | bicortex\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#article\",\"name\":\"SQL \\u2013 Calculating number of days in a month | bicortex\",\"headline\":\"SQL &#8211; Calculating number of days in a month\",\"author\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/wp-content\\\/post_content\\\/\\\/2012\\\/01\\\/SQL_Results_fun_GetDaysInMonth.png\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#articleImage\",\"width\":377,\"height\":205},\"datePublished\":\"2012-01-06T13:18:08+10:00\",\"dateModified\":\"2012-01-12T12:03:52+10:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#webpage\"},\"isPartOf\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#webpage\"},\"articleSection\":\"SQL, Code, SQL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\\\/\\\/bicortex.com\\\/bicortex\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/category\\\/sql\\\/#listItem\",\"name\":\"SQL\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/category\\\/sql\\\/#listItem\",\"position\":2,\"name\":\"SQL\",\"item\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/category\\\/sql\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#listItem\",\"name\":\"SQL &#8211; Calculating number of days in a month\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#listItem\",\"position\":3,\"name\":\"SQL &#8211; Calculating number of days in a month\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/category\\\/sql\\\/#listItem\",\"name\":\"SQL\"}}]},{\"@type\":\"Organization\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/#organization\",\"name\":\"bicortex\",\"description\":\"Business Intelligence & Analytics\",\"url\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/\"},{\"@type\":\"Person\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/author\\\/admin\\\/#author\",\"url\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/author\\\/admin\\\/\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3a576dc34dfe5a6c6f4be85d0bb81edc0ea83d4042b91e21337ef75c4e752c81?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"admin\"}},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#webpage\",\"url\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/\",\"name\":\"SQL \\u2013 Calculating number of days in a month | bicortex\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/#website\"},\"breadcrumb\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/sql-calculating-number-of-days-in-a-month\\\/#breadcrumblist\"},\"author\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2012-01-06T13:18:08+10:00\",\"dateModified\":\"2012-01-12T12:03:52+10:00\"},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/#website\",\"url\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/\",\"name\":\"bicortex\",\"description\":\"Business Intelligence & Analytics\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"http:\\\/\\\/bicortex.com\\\/bicortex\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"SQL \u2013 Calculating number of days in a month | bicortex","description":"","canonical_url":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#article","name":"SQL \u2013 Calculating number of days in a month | bicortex","headline":"SQL &#8211; Calculating number of days in a month","author":{"@id":"http:\/\/bicortex.com\/bicortex\/author\/admin\/#author"},"publisher":{"@id":"http:\/\/bicortex.com\/bicortex\/#organization"},"image":{"@type":"ImageObject","url":"http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/\/2012\/01\/SQL_Results_fun_GetDaysInMonth.png","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#articleImage","width":377,"height":205},"datePublished":"2012-01-06T13:18:08+10:00","dateModified":"2012-01-12T12:03:52+10:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#webpage"},"isPartOf":{"@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#webpage"},"articleSection":"SQL, Code, SQL"},{"@type":"BreadcrumbList","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex#listItem","position":1,"name":"Home","item":"http:\/\/bicortex.com\/bicortex","nextItem":{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex\/category\/sql\/#listItem","name":"SQL"}},{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex\/category\/sql\/#listItem","position":2,"name":"SQL","item":"http:\/\/bicortex.com\/bicortex\/category\/sql\/","nextItem":{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#listItem","name":"SQL &#8211; Calculating number of days in a month"},"previousItem":{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex#listItem","name":"Home"}},{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#listItem","position":3,"name":"SQL &#8211; Calculating number of days in a month","previousItem":{"@type":"ListItem","@id":"http:\/\/bicortex.com\/bicortex\/category\/sql\/#listItem","name":"SQL"}}]},{"@type":"Organization","@id":"http:\/\/bicortex.com\/bicortex\/#organization","name":"bicortex","description":"Business Intelligence & Analytics","url":"http:\/\/bicortex.com\/bicortex\/"},{"@type":"Person","@id":"http:\/\/bicortex.com\/bicortex\/author\/admin\/#author","url":"http:\/\/bicortex.com\/bicortex\/author\/admin\/","name":"admin","image":{"@type":"ImageObject","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/3a576dc34dfe5a6c6f4be85d0bb81edc0ea83d4042b91e21337ef75c4e752c81?s=96&d=mm&r=g","width":96,"height":96,"caption":"admin"}},{"@type":"WebPage","@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#webpage","url":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/","name":"SQL \u2013 Calculating number of days in a month | bicortex","inLanguage":"en-US","isPartOf":{"@id":"http:\/\/bicortex.com\/bicortex\/#website"},"breadcrumb":{"@id":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/#breadcrumblist"},"author":{"@id":"http:\/\/bicortex.com\/bicortex\/author\/admin\/#author"},"creator":{"@id":"http:\/\/bicortex.com\/bicortex\/author\/admin\/#author"},"datePublished":"2012-01-06T13:18:08+10:00","dateModified":"2012-01-12T12:03:52+10:00"},{"@type":"WebSite","@id":"http:\/\/bicortex.com\/bicortex\/#website","url":"http:\/\/bicortex.com\/bicortex\/","name":"bicortex","description":"Business Intelligence & Analytics","inLanguage":"en-US","publisher":{"@id":"http:\/\/bicortex.com\/bicortex\/#organization"}}]},"og:locale":"en_US","og:site_name":"bicortex | Business Intelligence &amp; Analytics","og:type":"article","og:title":"SQL \u2013 Calculating number of days in a month | bicortex","og:url":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/","article:published_time":"2012-01-06T13:18:08+00:00","article:modified_time":"2012-01-12T12:03:52+00:00","twitter:card":"summary","twitter:title":"SQL \u2013 Calculating number of days in a month | bicortex"},"aioseo_meta_data":{"post_id":"81","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[],"defaultGraph":"","defaultPostTypeGraph":""},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2020-12-23 05:06:01","updated":"2025-07-01 07:56:50","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"http:\/\/bicortex.com\/bicortex\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"http:\/\/bicortex.com\/bicortex\/category\/sql\/\" title=\"SQL\">SQL<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tSQL \u2013 Calculating number of days in a month\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"http:\/\/bicortex.com\/bicortex"},{"label":"SQL","link":"http:\/\/bicortex.com\/bicortex\/category\/sql\/"},{"label":"SQL &#8211; Calculating number of days in a month","link":"http:\/\/bicortex.com\/bicortex\/sql-calculating-number-of-days-in-a-month\/"}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":67,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":192,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/81\/revisions\/192"}],"wp:attachment":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}