(Note: DOB column (Datatype: DT_DBTIMESTAMP) has two date formats (DD/MM/YYYY & MM/DD/YYYY).) And when the above file extract the above file, data in the table looks like below.
Observations:
SSIS expects every date value to be in MM/DD/YYYY format & if it has an issue converting, then it assumes it to be in DD/MM/YYYY format. Hence 20/06/1988 didn’t fail & 03/04/2013 inserted as 4th March 2013.
If it couldn’t convert a date value to either of the formats (DD/MM/YYYY & MM/DD/YYYY), for example, 13/13/2008, then it throws an error “Data conversion failed: The value could not be converted because of a potential loss of data."
(Note: The above behavior is the same even if you use a Data conversion Transformation.)
We can overcome this default behavior of SSIS by declaring the Flat file source Datetime column datatype as String & Using DateTime.ParseExact () Function of C# to convert the string value to a specific Date format. (If the conversion fails, the error rows can be redirected to Error File. (Like Below)
Override the Input0_ProcessInputRow Function in the script component
do
{
try
String Name = Row.Name;
Int32 Age = Row.Age;
Decimal Salary = Row.Salary;
DateTime DateofB = DateTime.ParseExact(Row.DOB.ToString(),
"dd/MM/yyyy"
, System.Globalization.CultureInfo.InvariantCulture);
Output0Buffer.AddRow();
Output0Buffer.Age = Row.Age;
Output0Buffer.Name = Row.Name;
Output0Buffer.Salary = Row.Salary;
Output0Buffer.DateofB = DateofB;
}
catch
(Exception E)
ErrorBuffer.AddRow();
ErrorBuffer.Name = Row.Name;
ErrorBuffer.DOB = Row.DOB;
ErrorBuffer.ErrorDesc = E.Message;
while
(Row.NextRow());
if
(Row.EndOfRowset())
Output0Buffer.SetEndOfRowset();
Note: The Date format in the above code (Highlighted in Blue) can be sourced from Package configurations (SSIS String Variable). More about the format strings can be found here.
Rajkumar5055 edited Original. Comment: Formatting Done
Rajkumar5055 edited Revision 4. Comment: More Details Added
Maheshkumar S Tiwari edited Revision 5. Comment: Added TOC
Maheshkumar S Tiwari edited Revision 6. Comment: Added Tags and Formatted TOC as followed on TechNet Wiki
Ed Price - MSFT edited Revision 9. Comment: Font style; tags
Congrats on winning the gold medal: blogs.technet.com/.../technet-guru-awards-august-2013.aspx