Convert to NULL in case varchar in ssis - sql-server

From the source csv file, I have DailyTurnover column which is Int though there were \N values which correspond to NULL.
The question is - how can I convert this \N values to NULL going to destination column using SSIS task which has int data type?

I would handle this by adding a derived column that checks for \N and replaces it with a NULL int.
After your source item, add a Derived Column component. Change the "Derived Column" option to Replace 'DailyTurnover', and enter this expression (untested):
[DailyTurnover] == "\\N" ? NULL(DT_WSTR) : [DailyTurnover]
Then map the derived column to the destination.
EDIT: DT_I4 replaced with DT_WSTR in above expression based on error messages received by OP.

Related

SSIS : Conversion text stream DT_TEXT to DT_WSTR

I am creating a package in SSIS, and want to convert a file with one large column into multiple columns.
I have a table containing several rows with a single column of raw data. The data was copied from a notepad file, and each row contains pipe delimiters to separate each column, but because it is a notepad file, each row is copied as one large column. I want to convert each column per row to multiple columns based on their start/end positions.
I tried using SSIS Derived Column Transformation with the SUBSTRING function, but the Data Type is automatically populated as text stream[DT_TEXT], and I get the following error:
Error at [Derived Column[113]]; The function “SUBSTRING”
does not support the data type “DT_TEXT” for parameter number 1. The
type of the parameter could not be implicitly cast into a compatible
type for the function. To perform this operation, the operand needs
to be explicitly cast with a cast operator.
Error at [Derived Column[113]]; Evaluating function
'SUBSTRING' failed with error code 0xC0047089.
Error at [Derived Column[113]]; Computing the expression
"SUBSTRING[RawData],1,5)" failed with error code 0xC00470C5. The
expression may have errors, such as divide by zero, that cannot be
detected at parse time, or there may be an out-of-memory error.
Error at [Derived Column[113]]; The expression
"SUBSTRING[RawData], 1,5)" on "Derived Column.Outputs[Derived Column
Output].Coluns[Derived Column 1] is not valid
Error at [Derived Column[113]]; Failed to set property
"Expression" on "Derived Column.Outputs[Derived Column
Output].Columns[Derived Column 1]. "
When I review other Derived Column Transformation illustrations utilizing SUBSTRING with a file containing individual columns, I notice the Data Type is shown as DT_WSTR.
Do I need to convert to this Data Type? If so, how do I explicitly cast DT_TEXT data types to DT_WSTR with a cast operator in SSIS Derived Column Transformation?
Otherwise, how else could I handle this conversion?
Derived Column Name: EmployerNo
Derived Column: Replace 'RawData'
Expression: SUBSTRING( [RawData], 1, 5 )
Data Type: text stream[DT_TEXT]
I expect the RawData column to be split up (converted) into 8 different columns based on their start and end positions.
Refering to SUBSTRING (SSIS Expression) documentation:
Returns the part of a character expression that starts at the specified position and has the specified length.
You have to convert DT_TEXT column to DT_STR/DT_WSTR before using Substring() function, you can do this using a Script Component, you can use a similar function:
string BlobColumnToString(BlobColumn blobColumn)
{
if (blobColumn.IsNull)
return string.Empty;
var blobLength = Convert.ToInt32(blobColumn.Length);
var blobData = blobColumn.GetBlobData(0, blobLength);
var stringData = Encoding.Unicode.GetString(blobData);
return stringData;
}
Or if the DT_TEXT length doesn't exceed the DT_STR length limit try using the following SSIS expression:
SUBSTRING( (DT_STR,1252,4000)[RawData], 1, 5 )

Import from XML drops leading 0

I have an SSIS package that imports an XML file into SQL. The data of one particular field could be '112' or '039', for example. It is always three characters and gets padded with a leading 0 if only two.
The Destination field in SQL is varchar. For some or other reason SSIS is changing it to DT_UI2 and in the case of '039', only '39' comes.
I have added a data conversion that converts it to DT_WSTR but this does not help
Use a derived column with the following expression:
RIGHT("000" + (DT_WSTR,50)[Source Column],3)
The XSD that was originally generated defined this field as unsigned short. Changing it to string and redoing the flow solved the problem

Can we cast from string or char datatype to int (or numeric) in SSIS

I find this little challenging. I have source table and destination table.
Source table has one of the column with char datatype with length of 2.
Destination table has one of the column with int datatype.
Task is to migrate data from source to destination using SSIS package. And in that process I am using derived column transform to handle this conversion.
Column in Source table has value as "01" or "02" or "03". Whereas destination column having int datatype cannot be mapped with source column. As int only recognizes "1". I want 1 or 2 or 3 only and omit 0 in destination column.
I tried few things as below.
Try #1:
SUBSTRING(COLNAME, 2, 1)
This gives me "1" from "01" but when I do
(DT_R4) SUBSTRING(COLNAME, 2, 1 )
It fails me outright.
Try #2:
I try to cast like below.
(DT_DECIMAL, 2)(DT_R4) SUBSTRING(COLNAME, 2, 1 )
That did not work as well.
If there is a way out, I appreciate any direction here.
Since the destination column in of type Integer DT_I4 then you should use the following expression in the derived column:
(DT_I4)[SourceColumn]
Also check for Nulls and Empty strings:
ISNULL([SourceColumn]) ? NULL(DT_I4): ([SourceColumn] == "" ? NULL(DT_I4): (DT_I4)[SourceColumn])
Have you tried to directly connect source and destination columns without using the data conversion or derived column transformation? I think It should implicitly convert from char to Integer if all the data in the source table are numbers though the data type is char.

Converting Quoted string to integer?

I have an SSIS project where Flat File Source reads CSV file. It contains a field Order Item Id that is formatted as a string like "347262171", surrounded by quotes. I want to convert that to numeric value so I can use it as an index but everything I try gives me result:
Data conversion failed. The data conversion for column "Order Item ID" returned status value 2 and status text "The value could not be converted because of a potential loss of data."
What would be the easiest workaround for this?
You can add a Derived Column Transformation (DCT) to the data flow where you add an expression that removes quotes from the value:
REPLACE( [ID FIELD], "\"", "" )
where ID FIELD is the column with the ID value in your data. Add this column as a new NVARCHAR column to your data flow (ie STRIPPED_ID_FIELD).
Then, add a second DCT, where you cast this value to number (DB_NUMERIC(10,0))[STRIPPED_ID_FIELD], and name it NUM_ID_FIELD.
The reason I'd to this in a second, separate DCT, is that you can add an error output to this second one, and redirect that to a Recordset Destination. Then add a Data Viewer to the error output to see what sort of records are wrong. For instance, ID fields that have a letter that you're not expecting.
You can remove the double quotes in a Flat file connection by specifying Text Qualifier =" ,if you are using flat file .
Image description of where to insert Qualifier

SSIS Package - Derived Column Conditional Expression for Date Fields with Zero

I am relatively new to SSIS and its data types. I have successfully created a Data Flow task that imports data from a comma-delimited .txt flat file to SQL Server. An error occurs when running the task, at the point where a date field in the .txt file has 0.
For a Derived Column expression to convert the date fields with 0 to Null, I have come up with the following so far...
[Latest Bill Due Date]==0 ? NULL(DT_DATE) : (DT_DATE)[Latest Bill Due Date]
...but the logic isn't accepted and the error message appears:
The data types "DT_WSTR" and "DT_I4" are incompatible for binary operator "==". The operand types could not be implicitly cast into compatible types for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.
Thanks in advance for any direction.
I had a similar problem, when in a textfile there was a 00000000 value, had to convert it to null in a datetime column. What ended up working for me was stablishing the table with null value as default in the column and also adding a Script Component as Transformation. Add as a column output something like 'VerifyNullDateVar' and inside the script do something like
if (Row.DATEVAR == 0)
{
//do whatever you want to do if the input value is an actual date
Row.VerifyNullDateVar = 2;
}
else
Row.VerifyNullDateVar = 1;
DATEVAR is the input column you get from the textfile. After that, use a derived column to read the value from VerifyNullDateVar
VerifyNullDating == 1
VerifyNullDating == 2
Finally, you need to set up 2 OLEDB Destination, one when you can save a date value in the Table; and the other one when you ont save anything in it, that way it gets the default null value

Resources