{"id":177,"date":"2019-10-19T18:00:08","date_gmt":"2019-10-19T18:00:08","guid":{"rendered":"https:\/\/datablog.roman-halliday.com\/?p=177"},"modified":"2019-10-17T06:45:34","modified_gmt":"2019-10-17T06:45:34","slug":"dates-in-hive","status":"publish","type":"post","link":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/","title":{"rendered":"Dates In Hive"},"content":{"rendered":"\n<p>After previously spending a lot of time looking at dates in SQL Server and oracle, I wanted to do the same justice to Hive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Date Functions<\/h2>\n\n\n\n<p>Hive is closer to oracle than to SQL Server when it comes to formatting datas, here are some samples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">WITH sample_date_data AS\n(\nSELECT TIMESTAMP(from_unixtime(unix_timestamp())) AS the_timestamp\n)\nSELECT the_timestamp                      AS the_timestamp,\n       CAST(the_timestamp AS DATE)        AS the_date,\n     --TRUNC\n       TRUNC(the_timestamp, 'MM')         AS date_month,\n       TRUNC(the_timestamp, 'YY')         AS date_year,\n     --Common date parts\n       MONTH(the_timestamp)               AS month,\n       YEAR(the_timestamp)                AS year,\n     --DATE_FORMAT\n       DATE_FORMAT(the_timestamp, 'u')    AS daynumber_of_week,\n       DATE_FORMAT(the_timestamp, 'EEEE') AS day_name\n  FROM sample_date_data\n;<\/pre>\n\n\n\n<p>With the above example, one can also see the differentiation between a timestamp and a date (if you aren&#8217;t interested in the time, it&#8217;s easier not to store\/manipulate it).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TRUNC<\/h3>\n\n\n\n<p>Like oracle, hive has the <code>TRUNC<\/code> command which rounds down a date to the granularity specified. Unlike Oracle, this doesn&#8217;t include a default of day. For getting a whole day. I&#8217;d recommend casting to <code>DATE<\/code>. <\/p>\n\n\n\n<p>The <code>TRUNC<\/code> function is described as:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Returns date truncated to the unit specified by the format (as of Hive 1.2.0). Supported formats: MONTH\/MON\/MM, YEAR\/YYYY\/YY. Example: trunc(&#8216;2015-03-17&#8217;, &#8216;MM&#8217;) = 2015-03-01.<\/p><cite><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF\">Language Manual UDF<\/a><\/cite><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">DATE_FORMAT<\/h3>\n\n\n\n<p>Similar to Oracle and SQL Server equivalents, this is based on a Java library for converting\/representing values stored as dates in readable\/different formats.<\/p>\n\n\n\n<p>The <code>DATE_FORMAT<\/code> function is described as:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Converts a date\/timestamp\/string to a value of string in the format specified by the date format fmt (as of Hive 1.2.0). Supported formats are Java SimpleDateFormat formats \u2013 https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/text\/SimpleDateFormat.html. The second argument fmt should be constant. Example: date_format(&#8216;2015-04-08&#8217;, &#8216;y&#8217;) = &#8216;2015&#8217;.<\/p><cite> <a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF\">Language Manual UDF<\/a> <\/cite><\/blockquote>\n\n\n\n<p>The format strings for this come straight from Java, Some common format strings are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>EEEE<\/strong>: Day Name<\/li><li><strong>u<\/strong>: Day number of the week<\/li><li><strong>M<\/strong>: Month in year<\/li><\/ul>\n\n\n\n<p>There is a full list of date format strings over on the oracle Java website: <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/text\/SimpleDateFormat.html\">SimpleDateFormat<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After previously spending a lot of time looking at dates in SQL Server and oracle, I wanted to do the same justice to Hive.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[12,30],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-hive","tag-date","tag-hive"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dates In Hive - Rows Across The Lake<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dates In Hive - Rows Across The Lake\" \/>\n<meta property=\"og:description\" content=\"After previously spending a lot of time looking at dates in SQL Server and oracle, I wanted to do the same justice to Hive.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/\" \/>\n<meta property=\"og:site_name\" content=\"Rows Across The Lake\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-19T18:00:08+00:00\" \/>\n<meta name=\"author\" content=\"david\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:site\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"david\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/\"},\"author\":{\"name\":\"david\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"headline\":\"Dates In Hive\",\"datePublished\":\"2019-10-19T18:00:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/\"},\"wordCount\":283,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"keywords\":[\"date\",\"hive\"],\"articleSection\":[\"Hive\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/\",\"name\":\"Dates In Hive - Rows Across The Lake\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\"},\"datePublished\":\"2019-10-19T18:00:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/10\\\/19\\\/dates-in-hive\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dates In Hive\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\",\"name\":\"Rows Across The Lake\",\"description\":\"Data &amp; Databases\",\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\",\"name\":\"david\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"caption\":\"david\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dates In Hive - Rows Across The Lake","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:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/","og_locale":"en_GB","og_type":"article","og_title":"Dates In Hive - Rows Across The Lake","og_description":"After previously spending a lot of time looking at dates in SQL Server and oracle, I wanted to do the same justice to Hive.","og_url":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/","og_site_name":"Rows Across The Lake","article_published_time":"2019-10-19T18:00:08+00:00","author":"david","twitter_card":"summary_large_image","twitter_creator":"@d_roman_h","twitter_site":"@d_roman_h","twitter_misc":{"Written by":"david","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/#article","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/"},"author":{"name":"david","@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"headline":"Dates In Hive","datePublished":"2019-10-19T18:00:08+00:00","mainEntityOfPage":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/"},"wordCount":283,"commentCount":0,"publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"keywords":["date","hive"],"articleSection":["Hive"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/","url":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/","name":"Dates In Hive - Rows Across The Lake","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/#website"},"datePublished":"2019-10-19T18:00:08+00:00","breadcrumb":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/10\/19\/dates-in-hive\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/datablog.roman-halliday.com\/"},{"@type":"ListItem","position":2,"name":"Dates In Hive"}]},{"@type":"WebSite","@id":"https:\/\/datablog.roman-halliday.com\/#website","url":"https:\/\/datablog.roman-halliday.com\/","name":"Rows Across The Lake","description":"Data &amp; Databases","publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/datablog.roman-halliday.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b","name":"david","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","caption":"david"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g"}}]}},"_links":{"self":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":4,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/177\/revisions\/363"}],"wp:attachment":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}