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     

More DB2 Date Conversion Issues Importing Primary Keys into SQL Server

Published Wednesday, October 25, 2006 8:53 PM

In a previous blog entry (Date Errors Pulling DB2 Data into SQL Server) I recounted some of the issues I had with date conversions from DB2 to SQL Server.  Another interesting issue came up.  We were pulling data from a production DB2 database and continued getting duplicate primary key violations on a few records in some of the tables.  My initial reaction was that the client had done something crazy and somehow managed to really mess up their data.  But after taking a look at the tables in which the error was occurring, we found out that all of those tables had Date columns as part of the primary key.  DB2 dates apparently have more precision than SQL Server.  On some records, the loss of precision made some of the primary keys non-unique, causing the error.  Whoops.  Just another issue to look for if you're having DB2 conversion issues.

by Damon
Filed Under:

Comments

 

Tatsu said:

How precise is DB2 compared to SQL Server and how did you resolve the issue?
October 31, 2006 1:51 PM
 

Damon said:

If I remember correctly, SQL Server has an 8-byte date and DB2 has a 10-byte date.  DB2 also has a couple of different data types related to dates and times and I'm not sure which types have what storgae ranges.

If you want to avoid importing rows with invalid dates all-together, then you can use a filter on the DB2 side that excludes invalid date ranges for SQL.  For example,

WHERE [DateTimeCol] is NULL or DATE([DateTimeCol]) > DATE('1/1/1753')

Remember, though, you have to run this on the DB2 side because if SQL Server encounters an invalid date, it's going to throw an error before it ever checks that date against the where clause.

Another option is to convert the date to a string before you pass it to SQL Server.  Then you can build a custom function in SQL Server to read the string, determine if it's valid for SQL Server, and return an appropriate value.  This approach will be slower, however, because you are doing a lot of casting and calculations.

Hope that helps.

October 31, 2006 2:01 PM
You need to sign in to comment on this blog

















<October 2006>
SuMoTuWeThFrSa
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
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...