SSIS Flat File [DT_DBTIMESTAMP] cant convert to SQL datetime2 - sql-server

I created a running SSIS and I tried inserting data without error to SQL Server
but suddenly this error message pops up
Error: 0xC02020A1 at Data Flow Task, Flat File Source [59]: Data conversion failed. The data conversion for column "SCB_ActualDTime" returned status value 2 and status text "The value could not be converted because of a potential loss of data.".
It says that the error occurred in row 8 containing these data from the
SCB_ActualDTime
2017-04-16 15:28:07
It is really weird because I tried inserting the same data via SQL script and there is no error message.

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

Related

Converting String to Date in SSIS derived column

I have project parameter whose value will be like 'Group_AR_21Sep2021' and I want to fetch this date and put it into one of the column of the table which I am loading from Excel. So I put this in derived column (DT_DBDATE)(SUBSTRING(#[$Project::ARFileName],10,18)) but it is throwing me this error
[Derived Column [2]] Error: Casting expression "(SUBSTRING(#[$Project::ARFileName],10,18))" from data type "DT_WSTR" to data type "DT_DBDATE" failed with error code 0xC00470C2.
The second parameter to the substring function should be the length of the date: (DT_DBDATE)(SUBSTRING(#[$Project::ARFileName],10,9))
However, this would still fail because SSIS is super fragile when typecasting to date. I would suggest using a script component instead:
1 - Select the project parameter in the read only variables collection
2 - Add a column to the output columns and make it DT_DBDATE, i.e. NewDate
3 - Update the script to do the cast to the new column:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Row.NewDate = Convert.ToDateTime(Variables.ARFileName.Substring(9,9));
}

Converting text to date format SSIS

I am using SSIS to import data from Excel to SQL Server database.
The date column has nvarchar data type, and I want to change it to a date data type YYYY-MM-DD.
If the value is null then I want to get null.
I used [mycolumn] date, on the create a new table option on the OLE DB destination.
Is that right?
Add a Data Conversion Data Flow Component.
Right click, Show Advanced Editor...
Goto Input and Output Properties
Expand Data Conversion Output and click on the date column
Set FastParse to True in the Custom Properties
Make sure the data type is set to database date [DT_DBDATE]
Run the ssis package again the yyyymmdd dates should flow smoothly from nvarchar to date
Convert [mycolumn] to the needed DATE format before inserting it to the table. Try this:-
SELECT CONVERT(CHAR(10), [mycolumn],126)

wrong Dates values in ssis

I am working with ssis. I have a column that contains date in format integer YYYYMMDD: For example to day I get 20190214.I am using derived column to convert it in format date YYYY-MM-DD in my case for example 2019-02-14
But sometimes I receive wrong values for example 20188101. How could I test that I have good values to be converted to date?
There are 3 methods to achieve that
(1) Using Another Derived Column
Beside of the first Derived COlumn, you can add another derived column with the following expression:
(DT_DATE)(LEFT([DateColumn],4) + "-" + SUBSTRING([DateColumn],5,2) + "-" + RIGHT([DateColumn],2))
If the date value is incorrect redirect the bad from Error Output configuration , and you can handle these bad values from the Error Output.
Helpful Links
USING THE ERROR OUTPUT OF THE DERIVED COLUMN
How to prevent CAST errors on SSIS?
(2) Using a Script Component
Add a script component to check if the value is correct, and add a Output Column of type i.e. OutDateColumn, use a similar code (VB.NET)
IF Not Row.DateColumn_IsNULL Then
dim dtDate as DateTime
If DateTime.TryParseExact(Row.DateColumn,"yyyyMMdd",System.Globalization.InvariantCulture,System.Globalization.DateTimeStyles.None , dtDate ) Then
Row.outDateColumn = dtDate.ToString("yyyy-MM-dd")
Else
Row.outDateColumn_IsNull = True
End If
Else
Row.outDateColumn_IsNull = True
End If
(3) Add a Data Conversion Transformation
After the derived column Transformation, add a Data COnversion Transformation and try to convert the Date Derived Column you add to DT_DATE, if conversion failed, than handle the bad values from Error Output as explained in the first method.

SSIS ERROR "Conversion failed because the data value overflowed the specified type"

I'm using ssis to parse file txt into sql server. I use script task to convert a string example "20190523100520" (type :yyyy-MM-dd HH:mm:ss) to datetime (it's data type in table sql ).
But I get the error:
[OLE DB Destination [68]] Error: "There was an error with OLE DB
Destination.Inputs[OLE DB Destination Input].Columns[NewEffectiveTS_NXX_X] on
OLE DB Destination.Inputs[OLE DB Destination Input]. The column status
returned was: "Conversion failed because the data value overflowed the specified type.
In script task, tag inputs and outputs, i use database timestamp [DT_DBTIMESTAMP] type for Columns [NewEffectiveTS_NXX_X]
Everyone help me. Thank you so much!
To convert this value to datetime datatype inside the script you can use DateTime.ParseExact() function:
DateTime.ParseExact([Column],"yyyyMMddHHmmss",System.Globalization.CultureInfo.InvariantCulture)
Because the string value 20190523100520 cannot be implicitly converted to datetime by mapping a column of type string to another of type datetime.
Did you try to redirect the error output to a flat file and check if the rows contains wrong data in the datetime field.If you cant find any problem try to put the string in a valid format by inserting "/"," " and ":" for example your string "20190523100520" will become "2019/05/23 10:05:20"

How to make SSIS truncate numeric field in Flat File Destination

I'd like some advice about handling truncation issues in SSIS. I have a column Col1 which is MONEY in a table. I'd like to output that to a text file (fixed width, ragged right). In the output file, the column which holds Col1 must only be 8 characters wide.
In the OLEDB Data Source, Col1 is specified as:
currency [DT_CY] in both the External Columns and Output Columns tab.
In the Flat File Connection Manager's Advanced tab, Col1 is specified as:
currency [DT_CY], with InputColumnWidth set to 8.
If I populate Col1 with 123456789.00 and execute the task, the OLEDB source succeeds and passes rows to the destination, but the task fails with :
Error: 0xC02020A1 at DFT_Test, FFDEST_Test [3955]: Data conversion
failed. The data conversion for column "Col1" returned status value 4
and status text "Text was truncated or one or more characters had no
match in the target code page.". Error: 0xC02020A0 at DFT_Test,
FFDEST_Test [3955]: Cannot copy or convert flat file data for column
"Col1".
I want to avoid these truncation errors. In the Error Output of the source, I change the Truncation property for Col1 from Fail Component to Ignore Failure. I would have expected that would resolve the issue, but executing the task still gives the same error.
Can someone give some guidance about how to make SSIS simply truncate the column to 8 charactes?
Use a Derived Column task to create a column that is an 8-character string and populate it from the money column. Then in the Destination component, map the Derived Column to the Col1 Destination instead of the original column.
Or, even better, in your source component, use a SQL query that converts your money column to a varchar(8) or char(8) column.

Resources