{"id":1160,"date":"2014-06-11T16:23:17","date_gmt":"2014-06-11T06:23:17","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=1160"},"modified":"2014-08-14T00:59:06","modified_gmt":"2014-08-13T14:59:06","slug":"ssis-convert-datetimeoffset-datetime","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2014\/06\/11\/ssis-convert-datetimeoffset-datetime\/","title":{"rendered":"SSIS Convert between datetimeoffset and datetime"},"content":{"rendered":"<div id=\"ntsbl-1250039559\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Problem<\/h2>\n<p>I had a sql table that was storing data in a DateTimeOffset(7) datatype, DateDeleted.<\/p>\n<p>I was trying to use SSIS to sync the data to another table where the DateDelted column was a DATETIME datatype.<\/p>\n<p>How do you cast\/convert between a DateTimeOffset(7) datatype to a DateTime object<\/p>\n<h2>The Solution<\/h2>\n<p>In a derived column object use the following formula<br \/>\n[sql]<br \/>\nDATEADD(&quot;hh&quot;, DATEDIFF(&quot;hh&quot;,((DT_DBTIMESTAMP)DateDeleted), DateDeleted), ((DT_DBTIMESTAMP)DateDeleted))<br \/>\n[\/sql]<\/p>\n<p>I will walk you through what this actually is.<\/p>\n<h3>Part 1 &#8211; Base Cast<\/h3>\n<p>[sql]<br \/>\n(DT_DBTIMESTAMP) DateDeleted<br \/>\n[\/sql]<br \/>\nThis is just a standard type conversion.<br \/>\nThe problem!! <\/p>\n<p>When you try this you will receive a &#8220;Truncation Error&#8221;.<\/p>\n<ol>\n<li>Open the derived column transformation editor<\/li>\n<li>Click on &#8220;Configure Error Output&#8221;<\/li>\n<li>On this page for your derived column (in my case C_DateDeleted), set the Truncation value to &#8220;Ignore failure&#8221;<\/li>\n<\/ol>\n<p><a href=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1162\" data-permalink=\"https:\/\/ntsblog.homedev.com.au\/ssisconfigureerror-output\/\" data-orig-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output.png\" data-orig-size=\"764,247\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SSISConfigureError output\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output-300x96.png\" data-large-file=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output.png\" src=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output.png\" alt=\"SSISConfigureError output\" width=\"764\" height=\"247\" class=\"aligncenter size-full wp-image-1162\" srcset=\"https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output.png 764w, https:\/\/ntsblog.homedev.com.au\/wp-content\/uploads\/2014\/06\/SSISConfigureError-output-300x96.png 300w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/a><\/p>\n<p>So now it will work, but the problem is the offset is lost. In my case I am in Australia, +10 UTC. <\/p>\n<h3>Part 2 &#8211; Add the Offset back<\/h3>\n<p>So I want to add 10 hours back so I changed my derived column to <\/p>\n<p>[sql]<br \/>\nDATEADD(&quot;hh&quot;, 10, ((DT_DBTIMESTAMP)DateDeleted))<br \/>\n[\/sql]<br \/>\nThis works great, but what about daylight saving time when the offset may alter to 11 hours etc.<\/p>\n<h3>Part 3 &#8211; calculate the offset<\/h3>\n<p>[sql]<br \/>\nDATEDIFF(&quot;hh&quot;,((DT_DBTIMESTAMP)DateDeleted), DateDeleted)<br \/>\n[\/sql]<br \/>\nThis says get the hours difference between that DateTime version of the date ((DT_DBTIMESTAMP)DateDeleted) and the DateTimeOffset version of the date.<\/p>\n<h3>Part 4 &#8211; Final version<\/h3>\n<p>Now put it all together <\/p>\n<p>[sql]<br \/>\nDATEADD(&quot;hh&quot;, DATEDIFF(&quot;hh&quot;,((DT_DBTIMESTAMP)DateDeleted), DateDeleted), ((DT_DBTIMESTAMP)DateDeleted))<br \/>\n[\/sql]<\/p>\n<p>Easy :-)&#8230; not!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem I had a sql table that was storing data in a DateTimeOffset(7) datatype, DateDeleted. I was trying to use SSIS to sync the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1160","post","type-post","status-publish","format-standard","hentry","category-ssis"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1160"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1160\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}