{"id":32,"date":"2016-10-11T03:29:00","date_gmt":"2016-10-11T03:29:00","guid":{"rendered":"\/colin\/morethanfour\/post\/Dates-and-Time-Zone-conversion"},"modified":"2016-10-11T03:29:00","modified_gmt":"2016-10-11T03:29:00","slug":"dates-and-time-zone-conversion","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2016\/10\/11\/dates-and-time-zone-conversion\/","title":{"rendered":"Dates and Time Zone conversion"},"content":{"rendered":"<p>It&#8217;s time to take another look at some date\/time conversion techniques&#8230;<\/p>\n<p>Consider reading a DATE value from some source system, in a known time zone, and needing to convert it into some other time zone before saving into the destination table:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"border-top-style: none; font-size: 8pt; overflow: visible; border-left-style: none; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4\"><span style=\"color: #0000ff\">with<\/span><br>src_date <span style=\"color: #0000ff\">as<\/span> (<br>   <span style=\"color: #0000ff\">select<\/span><br>      <span style=\"color: #008000\">-- ---------------------------------------------------------------------<\/span><br>      <span style=\"color: #008000\">-- Here's a date value from the source system. From the value itself<\/span><br>      <span style=\"color: #008000\">-- we can't tell anything significant about it:<\/span><br>      <span style=\"color: #008000\">-- ---------------------------------------------------------------------<\/span><br>      to_date(<span style=\"color: #006080\">'2016-10-03 23:20'<\/span>,<span style=\"color: #006080\">'yyyy-mm-dd hh24:mi'<\/span>)         <span style=\"color: #0000ff\">as<\/span> INPUT_DT<br>   <span style=\"color: #0000ff\">from<\/span> dual<br>),<br>assume_data <span style=\"color: #0000ff\">as<\/span> (<br>   <span style=\"color: #0000ff\">select<\/span><br>      INPUT_DT,<br>      <span style=\"color: #008000\">-- --------------------------------------------------------------------------<\/span><br>      <span style=\"color: #008000\">-- If we know the value is a US\/Pacific time zone value, we can declare that:<\/span><br>      <span style=\"color: #008000\">-- --------------------------------------------------------------------------<\/span><br>      from_tz( <span style=\"color: #0000ff\">cast<\/span>( INPUT_DT <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #0000ff\">TIMESTAMP<\/span>) , <span style=\"color: #006080\">'US\/Pacific'<\/span> )   <span style=\"color: #0000ff\">as<\/span> PACIFIC_TSTZ,<br>      <span style=\"color: #008000\">-- --------------------------------------------------------------------------<\/span><br>      <span style=\"color: #008000\">-- Alternatively, what if we know it is in China Standard Time ?<\/span><br>      <span style=\"color: #008000\">-- --------------------------------------------------------------------------<\/span><br>      from_tz( <span style=\"color: #0000ff\">cast<\/span>( INPUT_DT <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #0000ff\">TIMESTAMP<\/span>) , <span style=\"color: #006080\">'+8:00'<\/span> )        <span style=\"color: #0000ff\">as<\/span> CHINA_TSTZ<br>   <span style=\"color: #0000ff\">from<\/span> src_date<br>)<br><span style=\"color: #0000ff\">select<\/span><br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   <span style=\"color: #008000\">-- Now we have values that declare what time zone they belong to,<\/span><br>   <span style=\"color: #008000\">-- we can easily transform the value into datetimes for a different time zone:<\/span><br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   INPUT_DT,<br>   PACIFIC_TSTZ,<br>   CHINA_TSTZ,<br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   <span style=\"color: #008000\">-- convert the China Standard Time value into US\/Central:<\/span><br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   CHINA_TSTZ <span style=\"color: #0000ff\">at<\/span> <span style=\"color: #0000ff\">time<\/span> <span style=\"color: #0000ff\">zone<\/span> <span style=\"color: #006080\">'US\/Central'<\/span>        <span style=\"color: #0000ff\">as<\/span> CENTRAL_TSTZ ,<br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   <span style=\"color: #008000\">-- convert the US\/Pacific time into UTC:<\/span><br>   <span style=\"color: #008000\">-- ----------------------------------------------------------------<\/span><br>   PACIFIC_TSTZ <span style=\"color: #0000ff\">at<\/span> <span style=\"color: #0000ff\">time<\/span> <span style=\"color: #0000ff\">zone<\/span> <span style=\"color: #006080\">'UTC'<\/span>             <span style=\"color: #0000ff\">as<\/span> UTC_TSTZ,<br>   PACIFIC_TSTZ <span style=\"color: #0000ff\">at<\/span> <span style=\"color: #0000ff\">time<\/span> <span style=\"color: #0000ff\">zone<\/span> <span style=\"color: #006080\">'+0:00'<\/span>           <span style=\"color: #0000ff\">as<\/span> UTC_TSTZ_ALT<br><span style=\"color: #0000ff\">from<\/span> assume_data;<\/pre>\n<p><\/div>\n<p>Results:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"display: inline\" alt=\"image\" src=\"http:\/\/www.spacefold.com\/colin\/morethanfour\/wp-content\/uploads\/image_7.png\" width=\"468\" height=\"148\"><\/p>\n<p>For example, when loading DATE values from C2R (Pacific time) and storing them in EDW (UTC) we could use the following expression:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"border-top-style: none; font-size: 8pt; overflow: visible; border-left-style: none; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4\"><span style=\"color: #0000ff\">cast<\/span>( from_tz(<span style=\"color: #0000ff\">cast<\/span>( INPUT_DT <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #0000ff\">TIMESTAMP<\/span>), <span style=\"color: #006080\">'US\/Pacific'<\/span>) <span style=\"color: #0000ff\">at<\/span> <span style=\"color: #0000ff\">time<\/span> <span style=\"color: #0000ff\">zone<\/span> <span style=\"color: #006080\">'UTC'<\/span> <span style=\"color: #0000ff\">as<\/span> <span style=\"color: #0000ff\">DATE<\/span> )<\/pre>\n<p><\/div>\n<p>That is, if we really needed a DATE data type. I believe Oracle DB would handle the outer cast automatically if we attempted to save the TIMESTAMP+TZ value into a destination table&#8217;s DATE column<\/p>\n<p>For more fun:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.oracle.com\/cd\/B19306_01\/server.102\/b14225\/ch4datetime.htm\">https:\/\/docs.oracle.com\/cd\/B19306_01\/server.102\/b14225\/ch4datetime.htm<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s time to take another look at some date\/time conversion techniques&#8230; Consider reading a DATE value from some source system, in a known time zone, and needing to convert it into some other time zone before saving into the destination table: withsrc_date as ( select &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212; Here&#8217;s a date value from the source [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-oracle","post-preview"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}