{"id":6673,"date":"2013-11-29T00:00:37","date_gmt":"2013-11-29T00:00:37","guid":{"rendered":"http:\/\/craftydba.com\/?p=6673"},"modified":"2016-04-20T14:31:23","modified_gmt":"2016-04-20T14:31:23","slug":"datename-time-related-functions","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=6673","title":{"rendered":"DATENAME() &#8211; Time Related Functions"},"content":{"rendered":"<p>I am unrelenting in my creation of short articles or tidbits on Transaction SQL <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms186724.aspx\">date\/time functions<\/a>. <\/p>\n<p>Most, if not all, modern day computer systems collect data which contains dates and times.  Many times you will want to convert the data from one data type to another.  <\/p>\n<p>Today, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174395.aspx\">DATENAME()<\/a> function.  This function has 15 different input parameters that can be passed to change the resulting output.  <\/p>\n<p>In short, the function takes a date part and date\/time variables as input returns the part that you want a character string data.  <\/p>\n<p>I will be exploring the four date part parameters that convert the variable to a time string.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"400\" align=\"center\">\n<thead>\n<tr style=\"color: #000000;background: #E0E0E0\">\n<td style=\"border: thin solid gray\">DATE PART<\/td>\n<td style=\"border: thin solid gray\">PARAMETER TO USE<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: thin solid gray\">HOUR<\/td>\n<td style=\"border: thin solid gray\">HH<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray\">MINUTE<\/td>\n<td style=\"border: thin solid gray\">MI or N<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray\">SECOND<\/td>\n<td style=\"border: thin solid gray\">SS or S<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray\">TIME ZONE OFFSET<\/td>\n<td style=\"border: thin solid gray\">TZ<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;\n<\/p>\n<p>The examples below are sample calls to the function.  The inputs puts are valid date, null value and a empty string.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"datename() - time related functions\">\r\n\r\n--\r\n--  Functions to get date and time parts as TEXT\r\n-- \r\n\r\n-- Hour ~ varchar(2) ~ 1 to 24\r\nSELECT \r\n  DATENAME(HH, SYSDATETIME()) AS HOUR_A_TXT2,\r\n  DATENAME(HH, NULL) AS HOUR_B_NULL,\r\n  DATENAME(HH, '') AS HOUR_C_EMPTY\r\nGO\r\n\r\n-- Minute ~ varchar(2) ~ 0 to 60\r\nSELECT \r\n  DATENAME(MI, SYSDATETIME()) AS MINUTE_A_TXT2,\r\n  DATENAME(N, SYSDATETIME()) AS MINUTE_B_TXT2,\r\n  DATENAME(MI, NULL) AS MINUTE_C_NULL,\r\n  DATENAME(N, '') AS MINUTE_D_EMPTY\r\nGO\r\n\r\n-- Second ~ varchar(2) ~ 0 to 60\r\nSELECT \r\n  DATENAME(SS, SYSDATETIME()) AS SECOND_A_TXT2,\r\n  DATENAME(S, SYSDATETIME()) AS SECOND_B_TXT2,\r\n  DATENAME(SS, NULL) AS SECOND_C_NULL,\r\n  DATENAME(S, '') AS SECOND_D_EMPTY\r\nGO\r\n\r\n-- Time Zone Offset ~ varchar(6)\r\nSELECT \r\n  SYSDATETIMEOFFSET() AS FULL_DATE4,\r\n  DATENAME(TZ, SYSDATETIMEOFFSET()) AS TZ_OFFSET_TXT6,\r\n  DATENAME(TZ, NULL) AS TZ_OFFSET_NULL,\r\n  DATENAME(TZ, '') AS TZ_OFFSET_EMPTY\r\nGO\r\n\r\n<\/pre>\n<\/p>\n<p>The output from the test is listed below.  It is surprising that an empty string returns a selected default value.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nHOUR_A_TXT2                    HOUR_B_NULL                    HOUR_C_EMPTY\r\n------------------------------ ------------------------------ ------------------------------\r\n14                             NULL                           0\r\n\r\n(1 row(s) affected)\r\n\r\nMINUTE_A_TXT2                  MINUTE_B_TXT2                  MINUTE_C_NULL                  MINUTE_D_EMPTY\r\n------------------------------ ------------------------------ ------------------------------ ------------------------------\r\n32                             32                             NULL                           0\r\n\r\n(1 row(s) affected)\r\n\r\nSECOND_A_TXT2                  SECOND_B_TXT2                  SECOND_C_NULL                  SECOND_D_EMPTY\r\n------------------------------ ------------------------------ ------------------------------ ------------------------------\r\n56                             56                             NULL                           0\r\n\r\nFULL_DATE4                         TZ_OFFSET_TXT6                 TZ_OFFSET_NULL                 TZ_OFFSET_EMPTY\r\n---------------------------------- ------------------------------ ------------------------------ ------------------------------\r\n2013-11-29 14:32:56.3175168 -05:00 -05:00                         NULL                           +00:00\r\n\r\n<\/pre>\n<\/p>\n<p>A brief summary of this article, pass one of the four ate part parameters to extract the time in a string format.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am unrelenting in my creation of short articles or tidbits on Transaction SQL date\/time functions. Most, if not all, modern day computer systems collect data which contains dates and times. Many times you will want to convert the data from one data type to another. Today, I will be exploring the DATENAME() function. This function has 15 different input parameters that can be passed to change the resulting output. In short, the function takes a date part and date\/time variables as input returns the part that you want a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6617,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[31,940,609,15,988,29],"class_list":["post-6673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-database-developer","tag-date-time-functions","tag-datename","tag-john-f-miner-iii","tag-time-related-parts","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6673"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6673\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/6617"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}