Converting Quoted string to integer? - sql-server

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

Related

ADF COPY ACTIVITY unable to identify the (") double quotes in between the column value while loading CSV files to snowflake

I'm facing the issue in ADF copy activity while loading the CSV data to the snowflake table,
The issue is while loading the CSV file to the snowflake table using the ADF COPY ACTIVITY, it is treating data of a single column as a multiple columns data,
for example: "My brother often watches different cricket shows on different ""screens"", but on the same different platform"
This is the value of single column_A but ADF copy activity is reading as a value for two-column instead of one
i.e col_A=My brother often watches different cricket shows on different ""screens"
col_B= but in the same different platform
But I want this value to be in single-column i,e column_A
column_A="My brother often watches different cricket shows on different ""screens"", but on the same different platform"
Any alternatives I could do for this?
In your source data, the column value contains comma , and double quotes " which are the same as your dataset properties column delimiter and Quote character.
Column delimiter is to separate the column based on the given delimiter value.
If the column value also contains the same delimiter character, the quote character is used to identify the complete value as a single column
Example:
sample data : "1,abc",def
Preview of data in Azure Data Factory dataset:
In your case you have both column delimiter and quotes character within your column value, so it is not identified as a single column but instead separated into 2 columns based on dataset property values (comma , and double-quotes ".)
Your sample data :
"My brother often watches different cricket shows on different ""screens"", but on the same different platform"
To fix this you can change the column delimiter in your source file or replace double quotes within column value with something else.
Example:

"EmptyHeader" in CSV Export Options?

I have a CSV file I am attempting to create, and the recipient requires a header row. In this header row (and in the data) there is a field that used to be present that was removed. However, they did not remove the column that that held that data, so now, there is an empty column name surrounded by delimiters ("|"). How can I recreate this?
The expected results for the following columns should be:
RxType1|RxType2|RxType3|RxType4|RxType5||DelivID
(There is an empty column between RxType5 and DelivID) and the results would be:
|Rx|OTC|Legend|Generic|Other||Express
I am using SSRS, and have attempted adding an extra pipe the the column header for RxType5 with an empty column behind it, but the CSV seems to generate a header row based on the column names from the stored procedure and not from the RDL data. I have also attempted in the Stored Proc to create the column by using:
Select
'' AS ""
OR
'' AS "|"
but when I refresh the fields in SSRS, it puts that the column is called "ID_" (because a space, no character, or pipe is non-CLS compliant.
Any suggestions on how I can achieve this? Thanks so much :)
Try creating the column with a known name, like SELECT '' AS [RemoveMe], and then just remove that name from the row header text box.

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 )

SSIS Data conversion transformation

in SSIS I read a csv file with column format - for example 1.25 or 2.50.
In the datatransformation task I transform into decimal dt_decimal scala 2.
In the datatable the column has the format decimal(18,2).
The data will be stored with 125.00 or 25.00 instead of 1.25 and 2.50.
What do I have to adjust?
Possible Issue causes
(1) Data type mismatch
I think that a similar issue is caused by data type mismatch between source and destination, or data transformation output and Destination.
(2) Numeric separators
Another cause may be if the numeric values contains a comma , as formatting such as thousands commas 1,000,000 and decimal separator . like 1.02.
Possible solutions
(1) Specify data type in source
To prevent this issue to be caused by data transformations and if your source data is formatted well. Then there is no need for Data Transformation. Inside the Flat File Connection Manager editor. GoTo Advanced Tab, Select the column that contains decimal and change its data type (try DT_NUMERIC and DT_DECIMAL) and precision and scale property.
If the issue still happening, be sure that both input and output has same metadata (precision and scale).
(2) Derived Column
Or you should use a Derived Column Transformation with a similar expression:
(DT_NUMERIC,18,2)[COLUMN]
(3) Replace Separator using derived column
You can replace separator using a derived column
(DT_NUMERIC,18,2)REPLACE([COLUMN], ",", ".")

Convert to NULL in case varchar in ssis

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.

Resources