{"id":442,"date":"2012-01-25T04:33:24","date_gmt":"2012-01-25T04:33:24","guid":{"rendered":"http:\/\/bicortex.com\/?p=442"},"modified":"2012-01-25T05:18:13","modified_gmt":"2012-01-25T05:18:13","slug":"sql-function-to-determine-day-type-work-day-vs-week-day","status":"publish","type":"post","link":"http:\/\/bicortex.com\/bicortex\/sql-function-to-determine-day-type-work-day-vs-week-day\/","title":{"rendered":"SQL Function to Determine Day Type (Work Day vs. Week Day)"},"content":{"rendered":"<p style=\"text-align: justify;\">Here is a little handy function which I use to either determine day type in some ad hoc query or populate date dimension in the data warehouse via SQL Server Integration Services (on that see the upcoming posts). All I do is pass the date parameter (in a &#8220;datetime&#8221; data type) to determine if the date is classed as a work day or weekend. It&#8217;s a really neat and simple solution as you don&#8217;t have to construct the code to determine the field value as part of the larger query; all it takes is just a simple function call. The code behind the solution as well as an execution sample is below:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n--------------------------------------------------------------------------------------\r\n--             CREATE A SCALAR-VALUED FUNCTION TO DETERMINE DAY TYPE CODE\r\n--------------------------------------------------------------------------------------\r\nCREATE FUNCTION &#x5B;dbo].&#x5B;Day_Type_Code]\r\n(@Date datetime)\r\n\r\nRETURNS varchar (10)\r\nAS\r\n    BEGIN\r\n             DECLARE @dtfirst int\r\n             DECLARE @dtweek int\r\n             DECLARE @iswkday varchar(10)\r\n\r\n             SET @dtfirst = @@datefirst - 1\r\n             SET @dtweek = DATEPART(weekday,@date)-1\r\n\r\n                   IF(@dtfirst + @dtweek)%7 not in (5,6)\r\n                        SET @iswkday = 'WD'\r\n                   ELSE\r\n                        SET @iswkday = 'WE'\r\n    RETURN @iswkday\r\nEND\r\n\r\nGO\r\n<\/pre>\n<p><a href=\"http:\/\/bicortex.com\/sql-function-to-determine-day-type-work-day-vs-week-day\/sql_results_fun_daytypecode\/\" rel=\"attachment wp-att-453\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-453\" title=\"SQL_Results_fun_DayTypeCode\" src=\"http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/\/2012\/01\/SQL_Results_fun_DayTypeCode.png\" alt=\"\" width=\"580\" height=\"630\" srcset=\"http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/2012\/01\/SQL_Results_fun_DayTypeCode.png 580w, http:\/\/bicortex.com\/bicortex\/wp-content\/post_content\/2012\/01\/SQL_Results_fun_DayTypeCode-276x300.png 276w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a little handy function which I use to either determine day type in some ad hoc query or populate date dimension in the data warehouse via SQL Server Integration Services (on that see the upcoming posts). All I do is pass the date parameter (in a &#8220;datetime&#8221; data type) to determine if the [&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-442","post","type-post","status-publish","format-standard","hentry","category-sql","tag-code","tag-sql"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/442","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=442"}],"version-history":[{"count":15,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":459,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/posts\/442\/revisions\/459"}],"wp:attachment":[{"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bicortex.com\/bicortex\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}