Flat File
is not a data source that force a specific data types for each column. so it may contains unicode characters or white spaces that prevent reading column as Datetime.
After working with many cases i decided to work with flat file in this way.
- In the
Flat File Connection Manager
i will read all column as DT_STR
column without specifying other data types
- Inside The
DataFlow Task
i will add a script component to check if columns value can be converted to the estimated data type and then converted it
Assuming that the date column name is inColumn
In the DataflowTask, Add a script component , Mark inColumn
as input column, add a new Output column outColumn
with dataType DT_DBTIMESTAMP
Change script language to vb.net

Mark your date column as Input

Create an Output column for each date column

Inside the script, in Input0_ProcessInputRow
sub use DateTime.ParseExact Function as follow:
In this example the date column is MailReceivingDate
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
If Not Row.MailReceivingDate_IsNull AndAlso
Not String.IsNullOrEmpty(Row.MailReceivingDate.Trim) Then
Dim dtDate as Date
If DateTime.TryParseExact(Row.MailReceivingDate.Trim, "yyyy-MM-dd HH:mm:ss", New System.Globalization.CultureInfo("en-GB"),System.Globalization.DateTimeStyles.None,dtDate) Then
Row.OutColumn = dtDate
Else
'If column cannot be parsed
Row.outColumn_IsNull = True
End If
Else
Row.outColumn_IsNull = True
End If
End Sub
End Class