Convert NA in "" values - static

I´m trying to convert NA values into blank values but without missing any of the rows. Using this code:
Data[is.na(Data)] <- ""
But it brings me this alert:
Error: Assigned data `""` must be compatible with existing data.
Error occurred for column `Grupo_empresarial_cód`.
Can't convert <character> to <double>.
Thanks for any help.

Related

What does this SSIS package data conversion error mean?

[Data Conversion [2]] Error: "Failed to convert data when converting LONGITUDE column (103) to" LONGITUDE copy "column (39). This conversion returned the status value 2 and the status text "The value could not be converted due to a potential loss of data. ".
"
[Data Conversion [2]] Error: "SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "Data conversion.Output [Data conversion output] .Columns [Copy of LONGITUDE]" object failed due to error code 0xC020907F. Additionally, the error line layout on "Data Conversion. Output [Data Output Data] .Columns [LONGITUDE Copy]" specifies a failure on the error. An error occurred on the specified object of the specified component. Error messages can be sent beforehand with information indicating the reason for the failure.
"
How can I correct it . I have tried changing data types and matching them .
I have checked my EXCEL file for syntax error ... nothing
Are you using Data Conversion transformation? If so, open it, and check the type of Copy of Longitude, compare that with source. Those two data type are not compatible. You could try to convert it to double.
If it is EXCEL destination, you need to convert it to DT_WSTR
you need to double all sizes in "data conversion" ,
first go to "source destination" and mark all nvarchar column
second go "data conversion" go to these columns and make it double size from length
Note : a data conversion by default make every column datatype 50

SSIS Flat File with Numeric DataType with Spaces

I'm using SSIS to load a fixed length Flat File into SQL.
I have a weight field that has been giving me trouble all day.
It has a length of 8 with 6 DECIMAL POSITIONS IMPLIED (99V990099).
The problem i'm having is when it isn't populated and has 8 spaces.
Everything i try gets an error:
"Invalid character value for cast specification"."
OR
"Conversion failed because the data value overflowed the specified type.".
OR
Data conversion failed.
The data conversion for column "REL_WEIGHT" returned status value 2 and status text
"The value could not be converted because of a potential loss of data.".
I've tried declaring it as DT_String & DT_Numeric.
I've tried many variations of:
TRIM([REL_WEIGHT])=="" ? (DT_STR,8,1252)NULL(DT_STR,8,1252) : REL_WEIGHT
ISNULL([REL_WEIGHT]) || TRIM([REL_WEIGHT]) == "" ? (DT_NUMERIC,8,6)0 : (DT_NUMERIC,8,6)[REL_WEIGHT]
TRIM(REL_WEIGHT) == "" ? (DT_NUMERIC,8,6)0 : (DT_NUMERIC,8,6)REL_WEIGHT
But nothing seems to work.
Please someone out there have the fix for this!
I think you may be running afoul of the following point, explained nicely at http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2009/02/01/ssis-expression-language-and-the-derived-column-transformation.aspx:
You can add a DT_STR Cast statement to the expression for the MiddleName, but it doesn't change the Data Type. Why can't we change the data type for existing columns in the Derived Column transformation? We're replacing the values, not changing the data type. Is it impossible to change the data type of an existing column in the Derived Column? Let's put it this way: It is not possible to convert the data type of a column when you are merely replacing the value. You can, however, accomplish the same goal by creating a new column in the Data Flow.
I've solved this on past occasions by loading the data from the flat file as strings, and then deriving a new column in a Derived Column transformation which is of numeric type. You can then perform the appropriate trimming, validation, casting, etc. in the SSIS expression for that new column in the transformation.
Here, I found an example SSIS expression I used at one point to derive a time value from a 4-digit string:
(ISNULL(Last_Update_Time__orig) || TRIM(Last_Update_Time__orig) == "") ? NULL(DT_DBTIME2,0) : (DT_DBTIME2,0)(SUBSTRING(TRIM(Last_Update_Time__orig),1,2)+":"+SUBSTRING(TRIM(Last_Update_Time__orig),3,2)+":00")
There has to be a better way to do it, But i found a way that works.
Create a Derived Column Expression:
TRIM(REL_WEIGHT) == "" ? (DT_STR,9,1252)"0.0000000" : (DT_STR,9,1252)(LEFT(REL_WEIGHT,2) + "." + RIGHT(REL_WEIGHT,6))
THEN Create a Data Conversion Task to change it to Numeric and set scale to 6.
And then Map the [Copy of NewField] to my SQL table field set up as Decimal(8,6).
I don't know how the performance will be of that when loading a million records, probably not the best. If someone knows how to do this in a better way performance wise please let me know.
Thanks,
Jeff

SSIS Blank Int Flat File Fail on Load

I have an SSIS package I am using to load a Fixed Width flat file. I have put in all the column lengths and have two packages against similar files working correctly. The third however keeps throwing the following error:
[Source 1 [16860]] Error: Data conversion failed. The data conversion for column "Line Number"
returned status value 2 and status text "The value could not be converted because of a
potential loss of data.".
[Source 1 [16860]] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.
The "output column "Line Number" (16957)" failed because error code 0xC0209084
occurred, and the error row disposition on "output column "Line Number" (16957)"
specifies failure on error. An error occurred on the specified object of the specified
component. There may be error messages posted before this with more information about
the failure.
After doing some testing this happens for any column I have the is using the DT_I4 Data Type and has a blank in the column. I was going to try using a derived column, but this seems to fail for some of the columns even if I change them to a string data type to handle the blank as a NULL and then do a conversion to an INT later in the data flow.
In the source and the destination task I have the Retain NULL values checkbox ticked however this hasn't changed anything.
Any suggestions on handling this error where INT seems to fail at converting a blank to a NULL?
DT_I4 maps to a four byte signed integer in SSIS.
You were on the right track with your derived column. You just need to add the right expression.
You can try this expression:
ISNULL([Line Number]) ? "0":[Line Number]
This link may also be of use - see the postcode column in the example
http://www.bidn.com/blogs/DonnyJohns/ssas/1919/handling-null-or-implied-null-values-in-an-ssis-derived-column
I ended up using the approach from this blog post:
http://www.proactivespeaks.com/2012/04/02/ssis-transform-all-string-columns-in-a-data-flow-stream/
to handle all of the null and blank columns via a script task and data conversion.

IronPython try/except not catching error

# the lines below are iterating through TIBCO Spotfire tables, getting the columns and the column properties, one of which is the column's "Calculated Expression" property
for tb in Document.Data.Tables:
for col in tb.Columns:
# initialize column variable
ces = ""
# get calculated expression and cast to string
try:
ces = col.Properties.CalculatedExpression.ToString()
exception:
ces="Error:Calculated Expression Not Read"
I know in advance that some of Calculated Expressions have unicode characters in them (can't do anything about that) so, I'm trying to "catch" those issues and simply write out the error.Then go to the next column.But, I continue to get the following error complaining about the greater than or equal to symbol:
System.Text.EncoderFallbackException: 'ascii' codec can't encode character '\u2264' in position 174
Doesn't explain it but what if you try:
for tb in Document.Data.Tables:
for col in tb.Columns:
# initialize column variable
ces = ""
# get calculated expression and cast to string
try:
ces = str(col.Properties.CalculatedExpression)
exception:
ces="Error:Calculated Expression Not Read"
instead?
If that doesn't work look into the unicode() function.
str() docs - https://ironpython-test.readthedocs.org/en/latest/library/functions.html#str
unicode() docs - https://ironpython-test.readthedocs.org/en/latest/library/functions.html#unicode
I'd test it myself if there was example data given.

SQL REPLACE giving error: 'String or binary data would be truncated.'

I'm trying to replace just part of a string in our company database. The column in which I'm trying to update is MERGECODES (varchar(20),null). A typical value of this column would be something like 'M, GPE, T'.
I would like to replace every instance of T with KD but I'm getting the error below. It will allow me to change anything with the same number of characters or less, for example, it will allow me to replace T with K but not with KD. Any help would be greatly appreciated. Thanks guys!
Code:
UPDATE GoldMine.dbo.CONTACT1
SET MERGECODES = REPLACE(MERGECODES, 'T', 'KD')
ERROR:
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.
You need to increase your data type size.
Currently you have varchar(20).
If the data is 20 characters long, and you replace 1 character for 2, then that will be 21 characters long, which will cause truncation.
Try increasing your data type to varchar(50) for example, and this should resolve your problem.

Resources