It is better to use a script component
instead of using a derived column
to achieve this because Scripts
contains more options to manipulate dates.
If you want to check for specific dates formats you can use DateTime.TryParseExact() method or you use DateTime.TryParse() to try parsing date based on CultureInfo Date Formats.
First you have to add a Script component
, mark the date column as input, create a new output column of type DT_DBTIMESTAMP
I will give two examples:
Assuming that the input Column name is inDate
and the output column is outDate
DateTime.TryParseExact()
Dim strFormats() As String = {"dd/MM/yyyy HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyyMMdd HH:mm:ss", "dd-MMM-yyyy HH:mm:ss", "MM/dd/yyyy HH:mm:ss"}
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
If Not Row.inDate_IsNull AndAlso
Not String.IsNullOrEmpty(Row.inDate) Then
Dim dtTemp As DateTime
If DateTime.TryParseExact(Row.inDate, strFormats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then
Row.outDate = dtTemp
Else
Row.outDate_IsNull = True
End If
End If
End Sub
DateTime.TryParse()
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
If Not Row.inDate_IsNull AndAlso
Not String.IsNullOrEmpty(Row.inDate) Then
Dim dtTemp As DateTime
If DateTime.TryParse(Row.inDate, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then
Row.outDate = dtTemp
Else
Row.outDate_IsNull = True
End If
End If
End Sub
you can read more about Datetime conversion using SSIS script component in this posts: