{"id":28,"date":"2017-01-11T14:32:00","date_gmt":"2017-01-11T14:32:00","guid":{"rendered":"\/colin\/morethanfour\/post\/Selecting-a-timestamp-AT-TIME-ZONE-a-gotcha"},"modified":"2017-01-11T14:32:00","modified_gmt":"2017-01-11T14:32:00","slug":"selecting-a-timestamp-at-time-zone-a-gotcha","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2017\/01\/11\/selecting-a-timestamp-at-time-zone-a-gotcha\/","title":{"rendered":"Selecting a timestamp AT TIME ZONE &#8211; a gotcha"},"content":{"rendered":"<p>So there&#8217;s this Oracle database instance in the midwest, in US\/Central time zone. <\/p>\n<p>So if I use PuTTY to open a terminal window on the host and open a SQL*Plus session, if I execute this statement I&#8217;ll get the following results:<\/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\">SQL<\/span>&gt; <span style=\"color: #0000ff\">select<\/span><br>       <span style=\"color: #0000ff\">max<\/span>( CHANGE_TS ) <span style=\"color: #0000ff\">as<\/span> MAX_CHANGE,<br>       <span style=\"color: #0000ff\">max<\/span>( CHANGE_TS <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> MAX_CST_CHANGE<br>       <span style=\"color: #0000ff\">from<\/span> tRetired_Mst;<br> <br>MAX_CHANGE                       MAX_CST_CHANGE<br>------------------------------<span style=\"color: #008000\">-- ---------------------------------------------<\/span><br>05-JAN-17 02.33.17.613000 PM     05-JAN-17 02.33.17.613000 PM US\/CENTRAL<br><\/pre>\n<p><\/div>\n<p>As expected, they are the same, because the local time zone of the session (terminal window) is the same as the DB,  <strong>US\/Central<\/strong>.<\/p>\n<p>However: <\/p>\n<p>If I run PL\/SQL Developer on my Windows Laptop here in California, and connect to the same Oracle instance, then the results are different:<\/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\">SQL<\/span>&gt; <span style=\"color: #0000ff\">select<\/span><br>       <span style=\"color: #0000ff\">max<\/span>( CHANGE_TS ) <span style=\"color: #0000ff\">as<\/span> MAX_CHANGE,<br>       <span style=\"color: #0000ff\">max<\/span>( CHANGE_TS <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> MAX_CST_CHANGE<br>       <span style=\"color: #0000ff\">from<\/span> tRetired_Mst;<br> <br>MAX_CHANGE                    MAX_CST_CHANGE<br>---------------------------<span style=\"color: #008000\">-- ----------------------------------------------<\/span><br>2017-01-05 14:33:17           05-JAN-17 04.33.17.613000 PM US\/CENTRAL<\/pre>\n<p><\/div>\n<p>Observe thatthe MAX_CST_CHANGE value is off by two hours! Because the timestamp value was evaluated as if it were in the local session time of my laptop, which is <strong>US\/Pacific<\/strong>.<\/p>\n<p>For code executing in PL\/SQL packages in the database, this is probably not an issue&#8230; but if you are using this technique to convert values in ad-hoc SQL, you should be aware of this trap.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So there&#8217;s this Oracle database instance in the midwest, in US\/Central time zone. So if I use PuTTY to open a terminal window on the host and open a SQL*Plus session, if I execute this statement I&#8217;ll get the following results: SQL&gt; select max( CHANGE_TS ) as MAX_CHANGE, max( CHANGE_TS at time zone &#8216;US\/Central&#8217; ) [&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-28","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\/28","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=28"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}