{"id":2354,"date":"2006-08-15T13:03:00","date_gmt":"2006-08-15T13:03:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/date-errors-pulling-db2-data-into-sql-server\/"},"modified":"2016-07-28T10:48:53","modified_gmt":"2016-07-28T10:48:53","slug":"date-errors-pulling-db2-data-into-sql-server","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/date-errors-pulling-db2-data-into-sql-server\/","title":{"rendered":"Date Errors Pulling DB2 Data into SQL Server"},"content":{"rendered":"<p>Moving data from DB2 to SQL Server has been an experience and a half, fraught with lessons throughout.&#160; My latest lesson is on dates.&#160; While running a query to import data, I kept running into this particular exception:&#160; <\/p>\n<p><strong>Error converting data type DBTYPE_DBTIMESTAMP to datetime<\/strong><\/p>\n<p>I figured there was some issue with one of the TIMESTAMP fields in the table.&#160; I quickly learned that DB2 called the date fields TIMESTAMPS, which I find bizzare since it means something different in SQL Server, but that&#8217;s another story.&#160; To temporarily get around the issue, I decided to exlude all of the date fields from the query so SQL Server wouldn&#8217;t choke.&#160; I ran it again.&#160; <\/p>\n<p><strong>Error converting data type DBTYPE_DBTIMESTAMP to datetime<\/strong><\/p>\n<p>What the heck?&#160; How can SQL Server be having an issue with a field that it&#8217;s not getting?&#160; Without going into all the detail, here&#8217;s what I found out.&#160; When you run a query against a linked server, SQL apparently goes out to the linked server and gets all of the data.&#160; It then brings it back to SQL Server for processing.&#160; If you exlude a particular column in your SQL Query, SQL Server STILL has to deal with the column because it&#8217;s pulling it back from the linked server.<\/p>\n<p>What had happened was someone botched a date (actually a few dates), accidentally entering 1\/1\/0200 instead of 1\/1\/2006.&#160; I don&#8217;t know what happened on 1\/1\/0200, but I know it was nothing relevant to what was in the database.&#160; Anyway, 1\/1\/0200 is a perfectly valid date for DB2 because it uses a 10 byte date value.&#160; SQL Server uses an 8 byte date value (don&#8217;t quote me on that), so the furthest back it goes is 1\/1\/1753, about 1500 years short of 1\/1\/0200.&#160; When DB2 passes 1\/1\/0200 to SQL Server, guess what you end up with?&#160; That&#8217;s right, Error converting data type DBTYPE_DBTIMESTAMP to datetime.&#160; <\/p>\n<p>So, how the heck do you get around it?&#160; Fortunately, you have an option in the OPENQUERY method.&#160; The OPENQUERY method accepts two parameters, the name of a linked server, and a string containing the query you want to run.&#160; When it runs, it passes the text of the query to the linked server and the linked server processes it.&#160;This allows you to create a WHERE clause that excludes (or transforms if you want) dates that occur before 1\/1\/1753.&#160; Since the query runs on the DB2 server, it doesn&#8217;t explode.&#160; And since it only returns valid SQL dates, SQL Server doesn&#8217;t blow up when the results come back.&#160; Here&#8217;s the general idea:<\/p>\n<p>SELECT * FROM OPENQUERY(DB2LinkedServerName, &#8216;SELECT * FROM [SomeTable] WHERE [SomeDateField] is NULL OR [SomeDateField] &gt; &#8221;1\/1\/1753&#8221;&#8217;);<\/p>\n<p>Who knew.&#160; Hope this keeps someone from breaking their monitor in frustration.&#160; I came close.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Moving data from DB2 to SQL Server has been an experience and a half, fraught with lessons throughout.&#160; My latest lesson is on dates.&#160; While running a query to import data, I kept running into this particular exception:&#160; Error converting data type DBTYPE_DBTIMESTAMP to datetime I figured there was some issue with one of the&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2354","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2354"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2354\/revisions"}],"predecessor-version":[{"id":41421,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2354\/revisions\/41421"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2354"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}