Damon Armstrong

Caffeine Induced Tirades about .NET and Life
And don't forget to check out my latest Simple-Talk articles
Add to Technorati Favorites      Add to Google     

Date Errors Pulling DB2 Data into SQL Server

Published Tuesday, August 15, 2006 1:03 PM

Moving data from DB2 to SQL Server has been an experience and a half, fraught with lessons throughout.  My latest lesson is on dates.  While running a query to import data, I kept running into this particular exception: 

Error converting data type DBTYPE_DBTIMESTAMP to datetime

I figured there was some issue with one of the TIMESTAMP fields in the table.  I quickly learned that DB2 called the date fields TIMESTAMPS, which I find bizzare since it means something different in SQL Server, but that's another story.  To temporarily get around the issue, I decided to exlude all of the date fields from the query so SQL Server wouldn't choke.  I ran it again. 

Error converting data type DBTYPE_DBTIMESTAMP to datetime

What the heck?  How can SQL Server be having an issue with a field that it's not getting?  Without going into all the detail, here's what I found out.  When you run a query against a linked server, SQL apparently goes out to the linked server and gets all of the data.  It then brings it back to SQL Server for processing.  If you exlude a particular column in your SQL Query, SQL Server STILL has to deal with the column because it's pulling it back from the linked server.

What had happened was someone botched a date (actually a few dates), accidentally entering 1/1/0200 instead of 1/1/2006.  I don't know what happened on 1/1/0200, but I know it was nothing relevant to what was in the database.  Anyway, 1/1/0200 is a perfectly valid date for DB2 because it uses a 10 byte date value.  SQL Server uses an 8 byte date value (don't quote me on that), so the furthest back it goes is 1/1/1753, about 1500 years short of 1/1/0200.  When DB2 passes 1/1/0200 to SQL Server, guess what you end up with?  That's right, Error converting data type DBTYPE_DBTIMESTAMP to datetime. 

So, how the heck do you get around it?  Fortunately, you have an option in the OPENQUERY method.  The OPENQUERY method accepts two parameters, the name of a linked server, and a string containing the query you want to run.  When it runs, it passes the text of the query to the linked server and the linked server processes it. This allows you to create a WHERE clause that excludes (or transforms if you want) dates that occur before 1/1/1753.  Since the query runs on the DB2 server, it doesn't explode.  And since it only returns valid SQL dates, SQL Server doesn't blow up when the results come back.  Here's the general idea:

SELECT * FROM OPENQUERY(DB2LinkedServerName, 'SELECT * FROM [SomeTable] WHERE [SomeDateField] is NULL OR [SomeDateField] > ''1/1/1753''');

Who knew.  Hope this keeps someone from breaking their monitor in frustration.  I came close.

by Damon
Filed Under:

Comments

 

lancefisher said:

I'm having this same problem on SQL Server 2005.  Another way around it is to cast the date to a string like:

SELECT * FROM OPENQUERY(DB2LinkedServerName, 'SELECT CAST([SomeDateField] AS varchar(26)) FROM [SomeTable])
August 17, 2006 3:54 PM
 

GlennMackey said:

"...DB2 calls all date fields TIMESTAMPS"  This is wrong, perhaps it was a typo, but DB2 has 3 date-type data-types. Date, Time and Timestamp. Perhaps your column was a timestamp. SQLServer treats all dates as a date-time.nnn - that is bizzare.
August 22, 2006 7:07 AM
 

Damon Armstrong said:

In a previous blog entry (Date Errors Pulling DB2 Data into SQL Server) I recounted some of the issues...
October 25, 2006 8:53 PM
 

Giacio1969 said:

thxanks damon and  lance for help me to solve this problem.
Simple and brilliant solution.
April 29, 2008 6:31 AM
You need to sign in to comment on this blog

















<August 2006>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789
Virtual Exchange Servers
 Microsoft now supports running Exchange Server 2007 in server virtualization environments, not just on... Read more...

Virtualizing Exchange: points for discussion
 With the increasing acceptance of the use of Virtualization as a means of providing server... Read more...

Encouraging .NET Reflector Add-ins
 Jason Haley is well-known for the resources he's provided to developers who wish to extend Reflector's... Read more...

Using .NET Reflector Add-ins
 .NET Reflector by itself is great, but it really comes into its own with the help of some add-ins. Here... Read more...

Unique Experiences!
 You'd have thought that a unique constraint was an easy concept - Not a bit of it; it can cause a lot... Read more...