SSIS 派生列转换转换错误

SSIS Derived Column Transformation Cast Error

我有一个 .txt 文件作为来源,下面是部分文件视图(所有价格列都显示为 DT_R4

Quantity    Partner Share   Customer Price
1           0               0
1           0               0
3           0.7             0.99
2           1.4             1.99
1          -1.4            -1.99

问题是我正在使用派生列创建一个新列,Discount,基于这些价格列,并且出于某种原因不想在价格为 0 时工作。派生下面的列转换:

(DT_NUMERIC,18,2)((ABS([Customer Price]) - [Partner Share]) / ABS([Customer Price]) * 100)

如果我忽略价格为 0 的行,一切正常,但我也需要它们:(

我猜可能是因为表达式将为 0/0 但我不知道如何修复它。

错误如下:

[Derived Column [297]] Error: An error occurred while attempting to perform a type cast.

[Derived Column [297]] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "component "Derived Column" (297)" failed because error code 0xC0049064 occurred, and the error row disposition on "output column "Discount" (376)" 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.

[SSIS.Pipeline] Error: SSIS Error Code DTS_E_PROCESSINPUTFAILED. The ProcessInput method on component "Derived Column" (297) failed with error code 0xC0209029 while processing input "Derived Column Input" (298). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running. There may be error messages posted before this with more information about the failure.

感谢任何帮助,谢谢!

尝试使用 TRY_CAST 运算符和 ISNULL 以防 0:

(DT_NUMERIC,18,2)((ABS( ISNULL(TRY_CAST([Customer Price] AS int), 0)) - [Partner Share]) 
     / ABS( ISNULL(TRY_CAST([Customer Price] AS int), 1)) * 100)

看起来有些字符串无法转换为整数。向您的数据流添加一些错误处理并将错误行路由到一个平面文件,以便您可以检查它们。

您可以尝试在 Derived Column 的表达式中使用 Conditional Operator,如下所示。

(DT_NUMERIC,18,2)((ABS([Customer Price]) - [Partner Share]) / ABS([Customer Price] == 0 ? 1 : [Customer Price]) * 100)

希望对你有用。