{"id":6535,"date":"2013-11-09T00:00:26","date_gmt":"2013-11-09T00:00:26","guid":{"rendered":"http:\/\/craftydba.com\/?p=6535"},"modified":"2016-04-20T17:58:00","modified_gmt":"2016-04-20T17:58:00","slug":"datetime-functions-year","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=6535","title":{"rendered":"Date\/Time Functions \u2013 YEAR()"},"content":{"rendered":"<p>I am going to hang on with writing my series 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.  <\/p>\n<p>Not only is it important to know when a sales order came in, but we should fulfill the request using some type of queue (<a href=\"http:\/\/en.wikipedia.org\/wiki\/LIFO_(computing)\">LIFO<\/a>).  If if was a stack (<a href=\"http:\/\/en.wikipedia.org\/wiki\/FIFO\">FIFO<\/a>) implementation, the first person may never get his order!<\/p>\n<p>Today, I will be exploring functions that take a datetime variable as input and return an integer as output.  <\/p>\n<p>The <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms186313.aspx\">YEAR()<\/a> function returns an integer for a given datetime variable.  Depending upon precision, the minimum return value could be 0001, 1753, or 1900 and the maximum value<br \/>\ncan be 2079 or 9999.<\/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=\"date\/time functions - year()\">\r\n--\r\n-- Extract part of date as an integer \r\n--\r\n\r\n-- Valid Year\r\nSELECT \r\n  YEAR(SYSDATETIME()) AS YEAR_INT\r\nGO\r\n\r\n-- Null Value\r\nSELECT \r\n  YEAR(NULL) AS YEAR_NULL\r\nGO\r\n\r\n-- Empty String\r\nSELECT \r\n  YEAR(' ') AS YEAR_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 value of 1900.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nYEAR_INT\r\n-----------\r\n2013\r\n\r\nYEAR_NULL\r\n-----------\r\nNULL\r\n\r\nYEAR_EMPTY\r\n-----------\r\n1900\r\n\r\n<\/pre>\n<\/p>\n<p>To rehash the article, this function returns the year from a datetime variable in integer format. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to hang on with writing my series 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. Not only is it important to know when a sales order came in, but we should fulfill the request using some type of queue (LIFO). If if was a stack (FIFO) implementation, the first person may never get his order! Today, I will be exploring functions that take a datetime variable as input and return&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[31,940,15,29,612],"class_list":["post-6535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-database-developer","tag-date-time-functions","tag-john-f-miner-iii","tag-tsql","tag-year"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6535","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=6535"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/6486"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}