Solved

DTS Import datetime field problem from Flat File SQL2000

Posted on 2006-06-25
13
864 Views
Last Modified: 2013-11-30
I am trying to import large flat files into my Table using DTS.

The import always failed due to the Date fields error:

TransformCopy 'DirectCopyXForm' conversion error: Conversion invalid for datatypes on column pair 27 (source column 'Col027' (DBTYPE_STR), destination column 'last_modified' (DBTYPE_DBTIMESTAMP))..... and so on for the other date fields that are in my table.  

My current solution, is to convert those fields to varchar and then import the file, then reconvert them back to datetime.  It works, but there has to be a better way, a right way to fix this problem??

Any suggestions??
0
Comment
Question by:hu8mypho
  • 7
  • 5
13 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Any suggestions??<<
Without knowing the date format and/or sample data we can only speculate.
0
 
LVL 1

Author Comment

by:hu8mypho
Comment Utility
Here is a sample of the date data that is being imported:

Nov 10 2005  2:57:42:000PM :  I format the field in the destination table as datetime but DTS will not work.  

If I convert to varchar then import it works.  Then afterwards, I have to convert back to datetime.

I hope this is enough info.  I can post more if needed.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
If you can lose the milliseconds than you use some ActiveX function like this:

Function GetDate (Byval Dat)
Dim Dat

Dat = Left (Dat, 20) & Mid(Dat, 25, 2)                   'Result: MMM dd yyyy hh:nn:ss?M

If IsDate(Dat) Then
  GetDate = CDate(Dat)
Else
   GetDate = Null
End If

You can then call it as follows:

DTSDestination("YourDateColumn") = GetDate(DTSSource("Col001"))
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
What is happening is that the Date variant in VBScript does not support milliseconds.  If the milliseconds are important to you than you will have to import the table as a varchar and then convert to a datetime using T-SQL functions.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
In order to convert from a string with milliseconds to a datetime using T-SQL you would use something like this:

CONVERT(datetime, 'Nov 10 2005  2:57:42:123PM', 109)
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
There is a transform of type 'DateTime String' in DTS. Have a play with that.


Alternatively, I am successfully importing datetime data in that format using BULK INSERT so you may wish to try using that instead of DTS.

Sample usage:

BULK INSERT YourTable
FROM 'D:\YourFile.TXT'
WITH (
FIELDTERMINATOR = ',',
TABLOCK
)

The count and datatype of fields need to exaclty match though.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:hu8mypho
Comment Utility
I'm going to try the activeX function, do I include it inside of the main function in the transformation script?

Alternatively, I have never used a bulk insert before, can you please specify?  
I have around 30 fields of which 5 are datetime formats, thanks for the help.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>do I include it inside of the main function in the transformation script?<<
It would be separate of the Main function in the same ActiveX Script Task as in:

Function Main()
      DTSDestination("YourDateColumn") = GetDate(DTSSource("Col001"))
      ' Rest of your columns go here
      Main = DTSTransformStat_OK
End Function

Function GetDate (Byval Dat)
Dim Dat

Dat = Left (Dat, 20) & Mid(Dat, 25, 2)                   'Result: MMM dd yyyy hh:nn:ss?M

If IsDate(Dat) Then
  GetDate = CDate(Dat)
Else
   GetDate = Null
End If

0
 
LVL 1

Author Comment

by:hu8mypho
Comment Utility
sample:"
      DTSDestination("columnx") = DTSSource("Col001")
      Main = DTSTransformStat_OK
End Function

Function GetDate (Byval Dat)
Dim Dat

Dat = Left (Dat, 20) & Mid(Dat, 25, 2)
If IsDate(Dat) Then
  GetDate = CDate(Dat)
Else
   GetDate = Null
End If  "

Error: "Activex Scripting Transform 'AxScriptXform': Error parsing script = Error Code: 0 Error Source=Microsoft VBScript compilation error Error Description" Name redefined "

Error on Line indicates the : "Function GetDate (Byval Dat)" line
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
Absolutely, left an extra line and left off the final line.  
Please delete the line: Dim Dat
Add the line at the end: End Function

Your function should look like this:

Function Main()
     DTSDestination("YourDateColumn") = GetDate(DTSSource("Col001"))
     ' Rest of your columns go here
     Main = DTSTransformStat_OK
End Function

Function GetDate (Byval Dat)

Dat = Left (Dat, 20) & Mid(Dat, 25, 2)                   'Result: MMM dd yyyy hh:nn:ss?M

If IsDate(Dat) Then
  GetDate = CDate(Dat)
Else
   GetDate = Null
End If

End Function
0
 
LVL 1

Author Comment

by:hu8mypho
Comment Utility
Sweet Thanks, It's importing...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Make sure to check after the import finalizes that the dates are converted correctly.  If the function GetDate does not understand the date than it will convert to Null.
0
 
LVL 1

Author Comment

by:hu8mypho
Comment Utility
Awesome, thank you its working, I'm kind of new to MS SQL .  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Server 2008 Cluster Fail-over Errors 5 40
Group by and order by clause 28 35
Mssql SQL query 14 24
Azure SQL DB? 3 11
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now