I'm creating SSIS packages to import large Fixed Width text documents and need to transform various fields. One such field represents dates and is 8 characters wide. The format is "yyyyMMdd" and like this:
SomeDateField
20130415
20110122
19891023
However, some fields have only blank spaces, and some will have all zeroes 00000000. Those that cannot be converted to a date should return a NULL value. I'm not allowed to substitute a date like 1/1/1900.
After futilely trying to convert this to a date field with expressions, I turned to C# Script Transformation.
My Input Column is called sSoldDate and is ReadOnly. I've configured an Output called SoldDate of DT-DBDATE data type.
The code I came up with that works is:
DateTime varDate;
bool isParsed;
isParsed = DateTime.TryParseExact(Row.sSoldDate, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out varDate);
if (isParsed)
{
Row.SoldDate = varDate;
}
else
{
Row.SoldDate_IsNull = true;
}
I then follow up with the other date fields like this one in the text import by repeating that code in the same Script Component and substituting the other field names. I don't have to repeat the variable declarations however.
Even though this works, I am going to have to repeat this process A LOT, and wanted to make sure this was the most expedient way of performing this kind of check and conversion. Is there a more concise way of achieving the same thing?
Is there a more performant way?
Is there some simple way to encapsulate the logic in a custom method or function that would make this process easier?