I want to convert string to int my string type [DT_WSTR] and my expression
ISNULL(col) || TRIM(col) == "" ? (DT_I4)0 : (DT_I4)col
I get this error [Derived Column [518]] Error: The conditional operation failed.
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityYou've got something that doesn't convert to an integer value in your source data.
Since the expression language doesn't have a tryparse method, the cleanest mechanism is to use a script component as a Transformation and leverage the .NET methods.
col
as ReadOnly. Add Column
Edit Script...
Line here so formatting picks up
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
int colOut = 0;
if (!Int32.TryParse(Row.col, out colOut))
{
Row.CleanCol_IsNull = true;
}
else
{
Row.CleanCol = colOut;
}
}
Here's my source query
SELECT
D.*
FROM
(
VALUES
(N'1')
, ('')
, ('0')
, ('3')
, ('X') -- Uncomment this one to generate error
, (NULL)
) D(col);
As I think about it a bit longer, in the above script, if you wanted to have zero outputted for "bad" values, replace the first action in the if statement with Row.CleanCol = 0;
Otherwise, this will make numbers of numbers and NULL of non-numbers.