问题
I got a CSV like below.
The column got some empty rows and the CSV gives the values as a string. But I want to make it a float like ‘3.00000’ with 5 decimals. First, I need to replace the ‘,’ symbol and replace the null values
I used this derived column expression: (DT_R8)REPLACE(ISNULL(Col1) ? "0.0000" : Col1,",",".")
but it is not working.
Do you guys know a better expression?
The column is like below:
+---------+
| Col1 |
+---------+
| 3,00000 |
| |
| 4,00000 |
| 6.56565 |
| |
| 4.54545 |
+---------+
回答1:
I believe that you will need to look at two things here. First, it is likely that the cells with a comma in them are recognized as strings and therefore they will use a text qualifier of quotes. If you don't explicitly set that up in your SSIS, then SSIS will keep the quotes and then you won't be able to convert that string to numeric. To confirm, on your connection manager, check your preview, if your numbers have a double quotes as below, then you need to set the text qualifier to a quote.
Snapshot before setting the text qualifier:
Now, if that's the case, you need to go to General, and make sure you text qualifier is set to " as below
Once done, you can verify that your preview is now fixed
The issue that you are having is assuming that you are dealing with NULL values. This isn't the case when your column is of type string imported from a flat file. You are dealing with an empty string here. So, you need to modify your expression to be:
(DT_R8)REPLACE(Col1 == "" ? "0.0000" : Col1,",",".")
Finally, do you recognize that replacing the "," with "." will result in "3,00000" being converted to 3.0 ? Please make sure that this is the expected behavior.
Following the above will result in the following derived column
来源:https://stackoverflow.com/questions/59251143/ssis-derived-column-from-csv-cast-string-to-float