Search code examples
sql-serverssiscastingdivide-by-zero

SSIS Derived Column Transformation Cast Error


I have a .txt file as a source, part of the file view is below (All price columns are coming as 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

The problem is that I’m using a derived column to create a new column, Discount, based on these price columns and for some reason doesn’t want to work when the prices are 0. Derived column transformation below:

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

If I ignore the rows where the prices are 0 everything it’s working fine, but I need those too :(

I have a guess that maybe it's because the expression will be 0/0 but I don't know how to fix it.

Below are the errors:

[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.

Any help is appreciated, thanks!


Solution

  • You can try to use Conditional Operator in expression of Derived Column something like below.

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

    Hope it works for you.