Click here to monitor SSC

Damon Armstrong

Caffeine Induced Tirades about .NET and Life
And don't forget to check out my latest Simple-Talk articles
View Damon Armstrong's profile on LinkedIn      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
 

xvang said:

Wow, you are awesome!  I sign-up just to post this one post.  Some user entered 0998 as a date (assuming it was meant as 1998) and it had me stuck for 8 hours at my desk trying to figure out why I couldn't create my SQL table from my DB2 data.
August 3, 2010 10:02 PM
You need to sign in to comment on this blog
Latest articles
Checking Out SQL Backup Pro 7’s New Automatic Backup Verification
 Wouldn't it be great to offload the daily chore of checking the integrity of your production... Read more...

Chuck Lathrope: DBA of the Day
 Chuck Lathrope was a finalist for the Exceptional DBA of the Year award in 2009. We contacted him to... Read more...

Backups, What Are They Good For?
 Pixar recently confessed, in an engaging video, that Toy Story 2 was almost lost due to a bad backup,... Read more...

C# Async: What is it, and how does it work?
 The biggest new feature in C#5 is Async, and its associated Await (contextual) keyword. Anybody who is... Read more...

SQL Server 2012 AlwaysOn
 SQL Server AlwaysOn provides a high-availability and Disaster-recovery solution for SQL Server 2012. It... Read more...